0

I'm new to JSF and facelets. I was wondering why am I not getting the expected result? I'm using JSF 1.2, with facelets 1.1.14. I'm expecting that the session variable 'indx' to hold the total number of grandchildren after the loop.

Instead its always 0. What am I doing wrong? Here is my code...

<h:form>
    <c:set var="indx" value="0" scope="session"></c:set>
    <ui:repeat value="#{grandparentholder.grandparents}" var="grandparent">
        <ui:repeat value="#{grandparent.parents}" var="parent">
             <ui:repeat value="#{parent.child}" var="child">
                 <c:set var="indx" value="#{indx+1}" scope="session"></c:set>
             </ui:repeat>
         </ui:repeat>
     </ui:repeat>
</h:form>
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

2 Answers2

1

JSF tags and JSTL tags does not run in sync as you'd expect from the coding. Long story short: JSTL in JSF2 Facelets... makes sense?

You have to solve your particular problem differently. How to do that depends on the concrete functional requirement. If I understand you correctly that you just want to count all available children, then you need <c:forEach> instead of <ui:repeat>

<c:set var="indx" value="0" scope="session" />
<c:forEach items="#{grandparentholder.grandparents}" var="grandparent">
    <c:forEach items="#{grandparent.parents}" var="parent">
         <c:forEach items="#{parent.child}" var="child">
             <c:set var="indx" value="#{indx+1}" scope="session" />
         </c:forEach>
     </c:forEach>
 </c:forEach> 

or to delegate the job to a session scoped backing bean and provide a getter to return the count.

private int indx;

public void init() {
    int indx = 0;

    for (Grandparent grandparent : grandparents) {
        for (Parent parent : grandparent.getParents()) {
            for (Child child : parent.getChild()) { // getChildren()??
                indx++;
            }
        }
    }

    this.indx = indx;
}

public int getIndx() {
    return indx;
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Taglibs ui and c have not the same life cycle.

You can't do this.

What do you want to do exactly ?

Kiva
  • 9,193
  • 17
  • 62
  • 94