Question

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">

  (...)

  <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?

Was it helpful?

Solution 3

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

OTHER TIPS

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">

  (...)

  <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>

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top