[英]How to print Hashmap values in xhtml page
I need your help and assistant in displaying each value in the Hashmap separately in an xhtml page. 在xhtml页面中分别显示Hashmap中的每个值时,我需要您的帮助和助手。 I tried to convert the hashmap to List, but I wasn't successful.
我试图将哈希图转换为List,但未成功。 My code is below:
我的代码如下:
private List<String> selectedTypes = new ArrayList<String>();
// The above list will store the selected types for example:(A,B)
Map<String, String> typesNames = new HashMap<String,String>(selectedTypes.size());
typesNames .put("A", "Type A");
typesNames .put("B", "Type B");
typesNames .put("C", "Type C");
for(String key: selectedTypes)
{
System.out.println(typesNames.get(key));
}
The result is showing in the console as: Type A Type B 结果在控制台中显示为:Type A Type B
So now my issue is that how to display the above output in the xhtml page in a list? 所以现在我的问题是如何在列表的xhtml页面中显示以上输出?
This should be work for it ; 这应该是可行的;
<c:forEach items="#{yourBean.map}" var="entry">
<li>Key: #{entry.key}, value: #{entry.value}</li>
</c:forEach>
----EDIT - - 编辑
private Map<String, String> typesNames ;
private List<String> selectedTypes;
public void fillMap() {
typesNames = new HashMap<String,String>();
typesNames .put("A", "Type A");
typesNames .put("B", "Type B");
typesNames .put("C", "Type C");
selectedTypes = new ArrayList<String>(typesNames .keySet());
}
public Map<String, String> getMap(){
return typesNames ;
}
public List<String> getKeyList(){
return selectedTypes;
}
output ; 输出;
<h:dataTable value="#{yourBean.selectedTypes}" var="key">
<h:column>
Key: #{key}
</h:column>
<h:column>
Value: #{yourBean.typesNames[key]}
</h:column>
</h:dataTable>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.