Troubles Iterating Over A HashMap with JSF, MyFaces & Facelets

Lee Theobald picture Lee Theobald · May 12, 2009 · Viewed 12k times · Source

I'm having some trouble looping over a HashMap to print out it's values to the screen. Could someone double check my code to see what I'm doing wrong. I can't seem to find anything wrong but there must be something.

In a servlet, I am adding the following to the request:

Map<String, String> facetValues = new HashMap<String, String>();
// Filling the map
req.setAttribute(facetField.getName(), facetValues);

In one case "facetField.getName()" evaluates to "discipline". So in my page I have the following:

<ui:repeat value="${requestScope.discipline}" var="item">
  <li>Item: <c:out value="${item}"/>, Key: <c:out value="${item.key}"/>, Value: <c:out value="${item.item}"/></li>
</ui:repeat>

The loop is ran once but all the outputs are blank?!? I would have at least expected something in item if it's gone over the loop once. Checking the debug popup for Facelets, discipline is there and on the loop. Printing it to the screen results in something that looks like a map to me (I've shortened the output) :

{300=0, 1600=0, 200=0, ... , 2200=0}

I've also tried with a c:forEach but I'm getting the same results. So does anyone have any ideas where I'm going wrong?

Thanks for any input, Lee

Answer

Berkay picture Berkay · Jun 7, 2012

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>