如何在xhtml页面中打印Hashmap值

时间:2015-04-03 10:05:17

标签: java arraylist xhtml hashmap

我需要你的帮助和助手在xhtml页面中单独显示Hashmap中的每个值。我试图将hashmap转换为List,但我没有成功。我的代码如下:

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));
    }

结果在控制台中显示为: A型 B型

所以现在我的问题是如何在列表的xhtml页面中显示上面的输出?

1 个答案:

答案 0 :(得分:1)

这应该是有用的;

<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;
    }       

输出;

   <h:dataTable value="#{yourBean.selectedTypes}" var="key"> 
        <h:column> 
            Key: #{key}
        </h:column> 
        <h:column> 
            Value: #{yourBean.typesNames[key]}
        </h:column> 
    </h:dataTable>