stackh34p
stackh34p

Reputation: 8999

Use c:set inside ui:repeat does not work

I have a JSF 2 application with the following code:

<c:set var="number" value="0" scope="view" />
<ui:repeat value="${items}" var="item" varStatus="itemIndex">
    <c:if test="${item.boolProperty == true}">
        <c:set var="number" value="${number + 1}" scope="view" />
    </c:if>
    <h:outputText value="#{item.name}" />: <h:outputText value="#{number}" />
</ui:repeat>

I want to increase the number depending on the property of the item in the loop. However, the condition seems not to work, since the value of number always stays 0. If I remove the condition, the number is incremented only once, or it is always 0 before incrementing, therefore it outputs 1. Could it be possible the number var changes not to affect the number var that is outside the loop? I believe the scope attribute takes care of that.

Upvotes: 0

Views: 4155

Answers (1)

BalusC
BalusC

Reputation: 1108632

JSTL tags and JSF components doesn't run in sync as you'd expect from the coding. JSTL tags runs during building of the JSF view, while JSF components runs during rendering of the JSF view. See for a detailed explanation also JSTL in JSF2 Facelets... makes sense?

What you want to achieve is unfortunately not possible with JSF components. The <ui:param> comes close, but it functions merely as an alias for a more complex EL expression, while the <c:set> actually sets something in the desired scope (the view scope which you've there is by the way wrong).

Your best bet is to change the model or wrap the model in another model so that you end up as

<ui:repeat value="${items}" var="item" varStatus="itemIndex">
    <h:outputText value="#{item.name}" />: <h:outputText value="#{item.number}" />
</ui:repeat>

Upvotes: 2

Related Questions