noup
noup

Reputation: 758

JSF JSTL problem with nested forEach

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?

Upvotes: 1

Views: 14123

Answers (3)

digitaljoel
digitaljoel

Reputation: 26584

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.

https://rogerkeays.com/jsf-c-foreach-vs-ui-repeat

Upvotes: 2

noup
noup

Reputation: 758

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

Upvotes: 0

Romain Linsolas
Romain Linsolas

Reputation: 81667

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>

Upvotes: 3

Related Questions