ui:repeat doesn't work with Map

前端 未结 4 863
滥情空心
滥情空心 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:36
    <a4j:repeat value="#{myBean.myMap.entrySet().toArray()}" var="_entry">
            <h:outputText value="#{_entry.key}"/><br/>
            <h:outputText value="#{_entry.value}"/>
    </a4j:repeat>
    

    also use with <ui:repeat>

    0 讨论(0)
  • 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 <T, S> List<Map.Entry<T, S>> mapToList(Map<T, S> map) {
    
        if (map == null) {
            return null;
        }
    
        List<Map.Entry<T, S>> list = new ArrayList<Map.Entry<T, S>>();
        list.addAll(map.entrySet());
    
        return list;
    }
    

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

    <namespace>http://example.com/util</namespace> 
    
    <function>
        <function-name>mapToList</function-name>
        <function-class>com.example.SomeClass</function-class>
        <function-signature>java.util.List mapToList(java.util.Map)</function-signature>
    </function>
    

    And finally use it on a Facelet like this:

    <html xmlns:util="http://example.com/util">
    
        <ui:repeat value="#{util:mapToList(someDate)}" var="entry" >
            Key = #{entry.key} Value = #{entry.value} <br/>
        </ui:repeat>
    
    0 讨论(0)
  • 2020-12-05 18:39

    with el 2.2 support you can iterate maps like below.

    <ui:repeat value="#{myBean.stats.keySet().toArray()}" var="x">
        <h:outputText value="#{myBean.stats.get(x)}" /><br />
    </ui:repeat>
    
    0 讨论(0)
  • 2020-12-05 18:41

    Seems to work for me on JSF 1.2, hope it helps someone...

        <h:panelGroup>
          <ul>
            <ui:repeat value="#{myBean.myMap.keySet().toArray()}" var="key">
              <li>key:#{key}</li>
              <li>value:#{myBean.myMap[key]}</li>
            </ui:repeat>
          </ul>
        </h:panelGroup>
    
    0 讨论(0)
提交回复
热议问题