在h:dataTable中使用java.util.Map [英] Using java.util.Map in h:dataTable
问题描述
我需要使用< h:dataTable>
显示 Map
。我的支持bean有一个 Map
属性如下:
I need to display Map
using <h:dataTable>
. My backing bean has a Map
property as below:
public class Bean {
private Map<Integer,String> map; // +getter
@PostConstruct
public void init() {
map = new TreeMap<Integer,String>();
map.put(1,"Sasi");
map.put(2,"Pushparaju");
map.put(3,"Venkat Raman");
map.put(3,"Prabhakaran");
}
}
然后在JSF页面中我正在尝试将映射
属性绑定到值
属性< h:dataTable>
。
Then in JSF page I am trying to bind this Map
property to the value
attribute of <h:dataTable>
.
<h:dataTable border="1" value="#{bean.map}" var="map">
<h:column id="column1">
<f:facet name="header">
<h:outputText value="UserId"></h:outputText>
</f:facet>
<h:outputText value="#{map.getKey}"></h:outputText>
</h:column>
<h:column id="column2">
<f:facet name="header">
<h:outputText value="Email Id"></h:outputText>
</f:facet>
<h:outputText value="#{map.getValue}"></h:outputText>
</h:column>
</h:dataTable>
它给出了错误, getKey
和 getValue
不存在。我可以理解,这不是正确的方法。如何使用< h:dataTable>
提交地图
?
It is giving en error that getKey
and getValue
is not present. I can understand that this is not the correct way to do it. How can I present a Map
using <h:dataTable>
?
推荐答案
直到即将到来的JSF 2.3, UIData
组件,如< h:dataTable> / code>,
< p:dataTable>
等和< ui:repeat>
强>不支持迭代地图
。这仅在< c:forEach>
中支持。
Until upcoming JSF 2.3, UIData
components such as <h:dataTable>
, <p:dataTable>
, etc and <ui:repeat>
does not support iterating over a Map
. This is only supported in <c:forEach>
.
一种方法是将地图条目转换为数组(单独 entrySet()
将不起作用 UIData
也不支持 Set
直到即将到来的JSF 2.3)。
One way is to convert the map entries to an array (alone entrySet()
won't work as UIData
also doesn't support Set
until upcoming JSF 2.3).
<h:dataTable value="#{bean.map.entrySet().toArray()}" var="entry">
<h:column>#{entry.key}</h:column>
<h:column>#{entry.value}</h:column>
</h:dataTable>
另一种方法是将地图的条目集合包含在集合中,这个集合中 ; h:dataTable>
可以迭代,例如 ArrayList
。
Another way is to wrap the map's entry set in a collection which the <h:dataTable>
can iterate over, such as an ArrayList
.
public class Bean {
private Map<Integer, String> map;
private List<Entry<Integer, String>> entries; // +getter (no setter necessary)
@PostConstruct
public void init() {
map = new TreeMap<>();
map.put(1, "Sasi");
map.put(2, "Pushparaju");
map.put(3, "Venkat Raman");
map.put(4, "Prabhakaran");
entries = new ArrayList<>(map.entrySet());
}
// ...
}
<h:dataTable value="#{bean.entries}" var="entry">
<h:column>#{entry.key}</h:column>
<h:column>#{entry.value}</h:column>
</h:dataTable>
然而,更干净,自我记录和可重用的是使用用户>
代替,其中用户
类具有必需的属性 id
和 name
。
However, more clean, self documenting and reusable is to use a List<User>
instead wherein the User
class has the necessary properties id
and name
.
public class Bean {
private List<User> users; // +getter (no setter necessary)
@PostConstruct
public void init() {
users = new ArrayList<>();
users.add(new User(1, "Sasi"));
users.add(new User(2, "Pushparaju"));
users.add(new User(3, "Venkat Raman"));
users.add(new User(4, "Prabhakaran"));
}
// ...
}
<h:dataTable value="#{bean.users}" var="user">
<h:column>#{user.id}</h:column>
<h:column>#{user.name}</h:column>
</h:dataTable>
这篇关于在h:dataTable中使用java.util.Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!