Saulius Šimčikas
Saulius Šimčikas

Reputation: 413

JSTL c:forEach does not iterate through a collection

Does anyone has any idea, why this code would work:

<h:outputText value="#{allocations[0].policyNumber}" />

and this code would work:

<c:forEach var="i" begin="1" end="5">
    <h:outputText value="aaa"/>
</c:forEach>

But this code would NOT work (nothing appears in GUI):

<c:forEach var="allocation" items="#{allocations}" >
    <h:outputText value="aaa"/>
</c:forEach>

I am using namespace "http://java.sun.com/jstl/core". allocations is a collection in java. I have tried everything I could think of and have no idea what to try next.

Upvotes: 3

Views: 5649

Answers (1)

BalusC
BalusC

Reputation: 1109695

The #{} notation is from unified EL. This is only supported in JSTL 1.2. Your JSTL namespace URI in combination with the fact that you're using JSF indicates that you're actually using JSTL 1.1.

You have 2 options:

  1. Use ${} notation instead. You should only guarantee that the bean is already present in the scope. The ${} notation namely won't auto-create managed beans (the #{} does that).

  2. Upgrade to JSTL 1.2. Download links and details can be found in our JSTL wiki page. Don't forget to change the XML namespace URI to http://java.sun.com/jsp/jstl/core.

Needless to say that option 2 is preferred. You should ban ${} from your JSF pages.

See also:


As a completely different alternative, you could also just use Facelets' own <ui:repeat> instead of the <c:forEach>. You should however understand the major difference that the Facelets one runs during view render time and that the JSTL one runs during view build time. See also JSTL in JSF2 Facelets... makes sense?

Upvotes: 5

Related Questions