ui:repeat doesn't work with Map

前端 未结 4 958
滥情空心
滥情空心 2020-12-05 18:20

I have a Map of key / values, which I initialize in @PostConstruct as follows:

Map myMap;

@PostConstruct
pub         


        
4条回答
  •  余生分开走
    2020-12-05 18:39

    UPDATE: JSF 2.3 (since 2017) supports this out of the box.

    Unfortunately, UIData and UIRepeat have no support for iterating over a map in JSF.

    If this bothers you (I guess it does), please vote for the following issue and if possible leave a comment that explains how you feel about this:

    http://java.net/jira/browse/JAVASERVERFACES_SPEC_PUBLIC-479

    In the mean time, you can iterate over a Map with some little helper code:

    /**
     * Converts a Map to a List filled with its entries. This is needed since 
     * very few if any JSF iteration components are able to iterate over a map.
     */
    public static  List> mapToList(Map map) {
    
        if (map == null) {
            return null;
        }
    
        List> list = new ArrayList>();
        list.addAll(map.entrySet());
    
        return list;
    }
    

    Then define an EL function in a *-taglib.xml file like this:

    http://example.com/util 
    
    
        mapToList
        com.example.SomeClass
        java.util.List mapToList(java.util.Map)
    
    

    And finally use it on a Facelet like this:

    
    
        
            Key = #{entry.key} Value = #{entry.value} 

提交回复
热议问题