显示JSF页面中的HashMap条目的键和值 [英] Display key and value of HashMap entries in JSF page
问题描述
我想在JSF UI中显示 HashMap
键及其关联的值。
我怎样才能做到这一点?如何使用迭代器组件(如< h:datatable>
??)在JSF页面中迭代 HashMap
只有< c:forEach>
支持
Map .Entry
实例返回(就像在普通的Java for
循环中)。 < c:forEach items =#{yourBean.map}var =entry>
< li>密钥:#{entry.key},值:#{entry.value}< / li>
< / c:forEach>
< h:dataTable>
(和< ui:repeat>
)仅支持 List (JSF 2.2将带有
Collection
支持)。您可以将所有密钥复制到单独的 List
中,然后对其进行迭代,然后使用迭代密钥使用 []
$ b
私人地图< String,String>地图;
私人列表< String>键列表;
public void someMethodWhereMapIsCreated(){
map = createItSomeHow();
keyList = new ArrayList< String>(map.keySet());
}
public Map< String,String> getMap(){
return map;
}
public List< String> getKeyList(){
return keyList;
< h:dataTable value =#{yourBean.keyList}var =key>
< h:栏>
键:#{key}
< / h:列>
< h:栏>
值:#{yourBean.map [key]}
< / h:列>
< / h:dataTable>
值得注意的是 HashMap
是自然无序。如果您想维护广告订单,请像 List
那样维持广告订单,而改为使用 LinkedHashMap
。
I would like to display HashMap
keys and its associated value in the JSF UI.
How can I achieve this? How can I iterate over a HashMap
in JSF page using some iterator component like <h:datatable>
?
Only <c:forEach>
supports Map
. Each iteration gives a Map.Entry
instance back (like as in a normal Java for
loop).
<c:forEach items="#{yourBean.map}" var="entry">
<li>Key: #{entry.key}, value: #{entry.value}</li>
</c:forEach>
The <h:dataTable>
(and <ui:repeat>
) only supports List
(JSF 2.2 will come with Collection
support). You could copy all keys in a separate List
and then iterate over it instead and then use the iterated key to get the associated value using []
in EL.
private Map<String, String> map;
private List<String> keyList;
public void someMethodWhereMapIsCreated() {
map = createItSomeHow();
keyList = new ArrayList<String>(map.keySet());
}
public Map<String, String> getMap(){
return map;
}
public List<String> getKeyList(){
return keyList;
}
<h:dataTable value="#{yourBean.keyList}" var="key">
<h:column>
Key: #{key}
</h:column>
<h:column>
Value: #{yourBean.map[key]}
</h:column>
</h:dataTable>
Noted should be that a HashMap
is by nature unordered. If you would like to maintain insertion order, like as with List
, rather use LinkedHashMap
instead.
这篇关于显示JSF页面中的HashMap条目的键和值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!