根据hashmaps列表动态生成h:列 [英] Dynamically generate h:column based on list of hashmaps
问题描述
在我的应用程序中,我想使用托管bean属性显示一个< h:dataTable>
。目前,此表是从列表< Folder>
创建的。现在我想将文件夹
更改为更动态的东西。这是因为如果我稍后决定添加另一个字段,我不想更改文件夹
类。我只需要在 Map< String,Object>
中添加另一个条目,而不是在文件夹
中引入一个新的字段。
In my application I want to display a <h:dataTable>
with managed bean properties. Currently this table is created from a List<Folder>
. Now I want to change the Folder
to something more dynamic. That's because I don't want to change the Folder
class if I decide to add another field later. I would just have to add another entry in the Map<String, Object>
instead of introducing a new field in Folder
.
所以,是否可以将 List< Map< String,Object>>
c $ c>< h:dataTable> ?
So, is it possible to bind a List<Map<String, Object>>
to the <h:dataTable>
?
推荐答案
可以将HashMaps列表绑定到jsf组件h:dataTable?
如果您使用视图构建时间标签(如JSTL < c:forEach> $ c)生成必要的
< h:column>
$ c>。
That's only possible if you generate the necessary <h:column>
tags with a view build time tag such as JSTL <c:forEach>
.
这是一个具体的开始示例,假设您的环境支持EL 2.2:
Here's a concrete kickoff example, assuming that your environment supports EL 2.2:
<h:dataTable value="#{bean.listOfMaps}" var="map">
<c:forEach items="#{bean.listOfMaps[0].keySet().toArray()}" var="key">
<h:column>
#{map[key]}
</h:column>
</c:forEach>
</h:dataTable>
(如果您的环境不支持EL 2.2,您需要提供另一个getter返回映射密钥设置为 String []
或 List< String>
;还要记住 HashMap
本质上是无序的,您可能需要使用 LinkedHashMap
来维护插入顺序)
(if your environment doesn't support EL 2.2, you'd need to provide another getter which returns the map key set as a String[]
or List<String>
; also keep in mind that a HashMap
is by nature unordered, you might want to use LinkedHashMap
instead to maintain insertion order)
当您使用早于2.1.18的Mojarra版本时,缺点是#{bean}
必须请求范围(不是查看范围)。或至少,< c:forEach items>
应该引用一个请求作用域bean。否则,在视图构建时间期间,当视图范围不可用时,视图范围bean将在每个单个HTTP请求上重新创建,因为< c:forEach>
运行。如果您绝对需要< h:dataTable>
的视图范围bean,那么您可以随时创建一个单独的请求作用域bean,用于 c:forEach items>
。解决方案是升级到Mojarra 2.1.18或更新版本。有关一些背景信息,另请参阅 JSF2 Facelets中的JSTL有意义?
When you're using Mojarra version older than 2.1.18, the disadvantage is that the #{bean}
has to be request scoped (not view scoped). Or at least, the <c:forEach items>
should refer a request scoped bean. A view scoped bean would otherwise be recreated on every single HTTP request as the <c:forEach>
runs during view build time, when the view scope isn't availabe yet. If you absolutely need a view scoped bean for the <h:dataTable>
, then you can always create a separate request scoped bean exclusively for <c:forEach items>
. The solution would be to upgrade to Mojarra 2.1.18 or newer. For some background information, see also JSTL in JSF2 Facelets... makes sense?
JSF组件库(如 PrimeFaces )可能会提供< x:columns>
标签,使得这更容易,例如 < p:dataTable>
with < p:columns>
。
JSF component libraries such as PrimeFaces may offer a <x:columns>
tag which makes this more easy, such as <p:dataTable>
with <p:columns>
.
<p:dataTable value="#{bean.listOfMaps}" var="map">
<p:columns value="#{bean.listOfMaps[0].keySet().toArray()}" var="key">
#{map[key]}
</p:columns>
</p:dataTable>
这篇关于根据hashmaps列表动态生成h:列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!