java - 如何在xhtml页面中打印Hashmap值

标签 java arraylist xhtml hashmap

我需要你的帮助和助手在 xhtml 页面中分别显示 Hashmap 中的每个值。我尝试将 HashMap 转换为列表,但没有成功。我的代码如下:

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页面中的上述输出?

最佳答案

这应该可行;

<c:forEach items="#{yourBean.map}" var="entry">
   <li>Key: #{entry.key}, value: #{entry.value}</li>
</c:forEach>

----编辑

    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>

关于java - 如何在xhtml页面中打印Hashmap值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29430065/

相关文章:

JavaFX 调用目标异常

java - 错误: Package does not exist Android Studio

java : Synchronising ArrayList for multiple operations

Java:将数组列表转换为带有添加元素的数组

java - 为什么向 ArrayList 添加变量时出现错误?

html - 是否每个带有 doctype 的 html 页面都需要互联网连接才能正确呈现页面?

java - 如何使用 Apache axis 在 Web 服务中使用 HashMap?

php - 回显一个 XHTML 标签

html 表单内容和 css 对齐问题

java - 在服务器上的 RESTful Web 服务上运行时出现 Liberty Server 错误