0

I'm using the repeat tag of JSF 2.0 to loop through a list of objects and display some of their properties. I want to use the varStatus attribute of repeat so that I can access the loop index, the number of the last list item, and to tell whether the end of the list has been reached (so the spacer won't be displayed). I thought this would work:

<ui:repeat var="anObject" varStatus="repeatStatus" value="#{objectList}">
    <h:panelGroup>
        <h:outputText value="Item #{repeatStatus.index + 1} of #{repeatStatus.end}" />
        <h:outputText value="#{anObject.text}" />
    </h:panelGroup>

    <h:outputText value="&nbsp;" rendered="#{false == repeatStatus.last}" />
</ui:repeat>

However, it never displays anything for repeatStatus.end. The index and last properties work well.

Instead of repeatStatus.end, I tried using objectList.size(), but that worked for only the first item in the list.

How can I display the number of items in the list as part of the "Item x of y" text?

Mr. Lance E Sloan
  • 3,297
  • 5
  • 35
  • 50

1 Answers1

2

The end is only used when you set the size attribute.

<ui:repeat ... size="#{fn:length(objectList)}">

Alternatively, you can also just use it directly.

Item #{repeatStatus.index + 1} of #{fn:length(objectList)}

By the way, the boolean comparison in #{false == repeatStatus.last} is ugly. It returns a boolean already; if you want to negate it, rather use #{not repeatStatus.last}.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • That makes sense. However, in this project I'm trying to avoid the use of JSTL. Based on your suggestion, I tried `size="#{objectList.size()}"`, but that had no effect. – Mr. Lance E Sloan Aug 01 '12 at 20:28
  • Create a custom EL function then. I however don't see how that would be better than just using an existing JSTL function. Perhaps you're overgeneralizing the one and other as to "avoiding JSTL in JSF". Read this Q&A then: http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense As to `.size()`, that works only if your environment uses EL 2.2 or JBoss EL. – BalusC Aug 01 '12 at 20:44
  • I've marked your answer as good even though I've not gotten it to work for me yet. I've never seen one of your solutions that didn't work. :) I'm a Java newbie, so I'm not sure why it's not working, but I'll figure it out. I verified what you said about the `size` attribute. If I put a number in there, `repeatStatus.end` does show that number. I began to think JSTL wasn't working, but then I tried `fn:toUpperCase` and that worked. I think the evaluation of `objectList` is at fault. I've had that problem before, but I think I need to open another question about that. – Mr. Lance E Sloan Aug 03 '12 at 19:23
  • Also, a big thanks for pointing me to the "JSTL in JSF2" question. I can't say I understand your answer completely, but I get the gist of it. As I said, I'm new to Java and JSF, so I was going by the advice of my colleague on this project. I think he was a little misinformed about this topic, too. – Mr. Lance E Sloan Aug 03 '12 at 19:25
  • I posted [another question](http://stackoverflow.com/questions/11802782/make-jsf-2-0-evaluate-method-as-they-are-assigned-to-params-not-store-reference) about parameters being assigned method references rather than their results. I feel like that's similar to the problem I have with getting `fn:length` to work. It's like JSF doesn't see a value for `objectList` at the point that `fn:length(objectList)` is called. `objectList` is supposed to be returned by a getter method. – Mr. Lance E Sloan Aug 03 '12 at 20:28