24

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

Map<String, String> myMap;

@PostConstruct
public void init() {

  myMap=new LinkedHashMap<String, String>();
  myMap.put("myKey","myValue");

}

public Map<String, String> getMyMap() {
    return myMap;
}

public void setMyMap(Map<String, String> myMap) {
    this.myMap = myMap;
}

When I try to iterate over this Map with <ui:repeat> like shown bellow, and I set a break point on the getter for the Map, I notice that it is not getting called, and so nothing is printed:

<ice:panelGroup>
    <ui:repeat items="#{myBean.myMap}" var="entry" varStatus="loop">
        <input type="checkbox" name="myCheckBoxes" value="#{entry.value}" />
        <span class="#{fn:contains(entry.value,'g') ? 'bold-style' : ''}">#{entry.key}</span>
    </ui:repeat>
</ice:panelGroup>

But when replacing above code with <c:foreach>, everything works fine, and the list is printed as expected, any ideas why I am getting such behavior?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498

4 Answers4

36

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>
Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • @Arjan Tijms, thanks for great answer, BTW, how can i vote up for the issue ? – Mahmoud Saleh Dec 18 '11 at 16:11
  • Create an account and login (it's quick and easy), then on the left hand side of the page, below "operations" there's a "voting" link somewhere in the middle of the list of links. – Arjan Tijms Dec 18 '11 at 16:21
  • little more info about creating new taglib file please. – Mahmoud Saleh Dec 19 '11 at 07:40
  • well i define it as in this post http://stackoverflow.com/questions/2192759/concatenate-strings-in-jsf-jsp-el-and-javascript but it doesn't work it cannot find my method. – Mahmoud Saleh Dec 19 '11 at 09:12
  • 1
    @Msaleh I think it's best to create a new question on how to create EL functions. On SO it's the norm to keep questions as focussed on a single topic as possible. – Arjan Tijms Dec 19 '11 at 19:33
  • 1
    Hi @ArjanTijms, I think it might be a good idea to mention that this wasn't achievable in early stages of JSF 2, but it's actually possible right now. Just for newcomers. – Aritz Jul 15 '18 at 17:23
27
<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>

user1483443
  • 266
  • 3
  • 4
24

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>
c30
  • 241
  • 2
  • 2
10

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>
Berkay
  • 1,438
  • 13
  • 8
  • 7
    This is very inefficient as the full map lookup happens again during every `get(x)`. Better iterate over `entrySet()`. – BalusC Apr 16 '15 at 11:18