tags:

views:

2804

answers:

3

Inside a nested foreach, accessing the same variable is returning different values. This happens when the page is reloaded, not on first load.

<ui:composition
  xmlns="http://www.w3.org/1999/xhtml"
  (...)
  xmlns:c="http://java.sun.com/jstl/core"
  xmlns:h="http://java.sun.com/jsf/html"&gt;

  (...)

  <c:forEach items="#{controller.availableTransitions}" var="transition">
    <c:forEach items="#{transition.availableTransitions}" var="transitionItem">
      <h:outputText value="1_#{transitionItem.name} 2_#{transitionItem.name}" />
      3_#{transitionItem.name} 4_#{transitionItem.name}
    </c:forEach>
  </c:forEach>
</ui:composition>

After page reload, transitionItem.Name returns the correct value for 3 and 4, and different values for 1 and 2. Maybe a JSF-JSTL integration problem?

+1  A: 

I see that you are using Facelets.

Maybe you can try to replace your <c:forEach> by <ui:repeat>...

The code will then become:

<ui:composition
  xmlns="http://www.w3.org/1999/xhtml"
  (...)
  xmlns:c="http://java.sun.com/jstl/core"
  xmlns:h="http://java.sun.com/jsf/html"&gt;

  (...)

  <ui:repeat value="#{controller.availableTransitions}" var="transition">
    <ui:repeat value="#{transition.availableTransitions}" var="transitionItem">
      <h:outputText value="1_#{transitionItem.name} 2_#{transitionItem.name}" />
      3_#{transitionItem.name} 4_#{transitionItem.name}
    </ui:repeat>
  </ui:repeat>
</ui:composition>
romaintaz
I've tried that, but the result is the same.
noup
A: 

Found a workaround, by getting rid of the inner forEach loop, thus returning a linear list from the controller.

noup
+2  A: 

In general, I try to use ui:repeat most of the time. When I was having c:set issues, I found this blog, which was very helpful and may apply in your case also.

http://www.ilikespam.com/blog/c:foreach-vs-ui:repeat-in-facelets

digitaljoel