johnny-b-goode
johnny-b-goode

Reputation: 3893

Problems in usage of JSP's <c:forEach>

I am trying to iterate through Collection of items:

<c:forEach items="#{tree.items}" var="item">
    <h:commandLink rendered="#{item.type == 'category'}" action="#{item.onNodeClicked}"

Hovewer, i've got an exception:

javax.el.PropertyNotFoundException: The class 'java.lang.String' does not have the property 'type'.

Looks like JSP recognizes tree items as String objects. What i am missing?

Upvotes: 1

Views: 297

Answers (1)

BalusC
BalusC

Reputation: 1108632

This is not going to work. The #{item} is only available during view build time, while the rendered attribute is evaluated during view render time.

Use <c:if> instead.

<c:forEach items="#{tree.items}" var="item">
    <c:if test="#{item.type == 'category'}">
        <h:commandLink ... action="#{item.onNodeClicked}" />

Or, if you're on JSF2 (which is out for more than 3 years), replace that legacy JSP by its successor Facelets and use its <ui:repeat> component instead. It's evaluated during view render time as well.

<ui:repeat value="#{tree.items}" var="item">
    <h:commandLink rendered="#{item.type == 'category'}" action="#{item.onNodeClicked}" />

Upvotes: 1

Related Questions