当您使用早于 2.1.18 的 Mojarra 版本时,缺点是 #{bean} 必须是请求范围的(而不是视图范围的).或者至少, 应该引用一个请求范围的 bean.否则,视图范围 bean 将在每个 HTTP 请求上重新创建,因为 在视图构建期间运行,此时视图范围尚不可用.如果您绝对需要 <h:dataTable> 的视图范围 bean,那么您始终可以为 <c:forEach items> 创建一个单独的请求范围 bean.解决方案是升级到 Mojarra 2.1.18 或更新版本.有关一些背景信息,另请参见 JSF2 Facelets 中的 JSTL ......有意义吗? JSF 组件库,例如 PrimeFaces 可能会提供 标记使这更容易,例如 与 .<p:columns value="#{bean.listOfMaps[0].keySet().toArray()}" var="key">#{地图[键]}</p:columns></p:dataTable>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. So, is it possible to bind a List<Map<String, Object>> to the <h:dataTable>? 解决方案 That's only possible if you generate the necessary <h:column> tags with a view build time tag such as JSTL <c:forEach>.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>(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)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 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:column的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 15:17