duan19780629 2013-01-29 12:16
浏览 49
已采纳

用于PHP的StdClass的Java模拟

I need something like a property bag to throw key value pairs into. I want to create list of such objects, initialize them in bean and then use the list to render table in JSF template. I know how to do it, but I want to avoid creating some special class for that case, like OrderLineItem and using List<OrderLineItem>. I do not want to define new class.

In PHP I could use StdClass.
StdClass is a sparsely documented class in PHP which has no predefined members.

$object = new StdClass;
$object->foo = 'bar';

But, as far as I know, Primefaces <p:dataTable> list item must be an object with getter. If I want to reference <h:outputText value="#{item.title}" />, my list object should have getTitle() method.

Is there any walkaround in my case or I really need to define special class to make life easier?
Thanks.

  • 写回答

4条回答 默认 最新

  • dqnhfbc3738 2013-01-29 12:18
    关注

    When you want a simple key/value table, then the HashMap might be what you are looking for.

    Map<String, String> myMap = new HashMap<>();
    
    myMap.put("foo", "bar");
    
    System.out.println(myMap.get("foo")); // outputs "bar"
    

    This example matches Strings to Strings, but you can use HashMaps to map any type to any other type. You can even create a Map<Object, Object> to create a weakly-typed map which maps anything to anything. But for most use-cases you would rather use a more specialized form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?