I am trying to process the LAST item in a findResources query differently from all others, but I can't fugure out how to do that. Here is a code snippet that I'm trying out (findResources returns THREE 'telephone' objects in my JCR) :-
<sling:findResources query="/jcr:root ${currentPage.path} //*[${tagCriteria} @sling:resourceType = 'avivapocs/components/telephone']" language="xpath" var="resources" /> <c:forEach var="telephoneResource" items="${resources}"> <c:choose> <c:when test="${resources}.hasNext()"> DO SOMETHING WITH ALL OTHER telephone resource ITEMS </c:when> <c:otherwise> DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM </c:otherwise> </c:choose> </c:forEach>
But the 'otherwise' path is chosen in ALL cases.
Anyway, I am a bit stumped .. so any advice would be welcome.
Regards
Fraser.
Solved! Go to Solution.
Views
Replies
Total Likes
Hi,
The correct way to do this with the c:forEach tag is to use the varStatus attribute to capture the status object (of type http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/c...). It would look something like this:
<c:forEach var="telephoneResource" items="${resources}" varStatus="status"> <c:choose> <c:when test="${status.last}"> DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM </c:when> <c:otherwise> DO SOMETHING WITH ALL OTHER telephone resource ITEMS </c:otherwise> </c:choose> </c:forEach>
Regards,
Justin
Views
Replies
Total Likes
Hi,
The correct way to do this with the c:forEach tag is to use the varStatus attribute to capture the status object (of type http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/c...). It would look something like this:
<c:forEach var="telephoneResource" items="${resources}" varStatus="status"> <c:choose> <c:when test="${status.last}"> DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM </c:when> <c:otherwise> DO SOMETHING WITH ALL OTHER telephone resource ITEMS </c:otherwise> </c:choose> </c:forEach>
Regards,
Justin
Views
Replies
Total Likes
justin_at_adobe wrote...
Hi,
The correct way to do this with the c:forEach tag is to use the varStatus attribute to capture the status object (of type http://docs.oracle.com/cd/E17802_01/products/products/jsp/jstl/1.1/docs/api/javax/servlet/jsp/jstl/c...). It would look something like this:
<c:forEach var="telephoneResource" items="${resources}" varStatus="status">
<c:choose>
<c:when test="${status.last}">
DO SOMETHING DIFFERENT WITH THE LAST telephone resource ITEM
</c:when>
<c:otherwise>
DO SOMETHING WITH ALL OTHER telephone resource ITEMS
</c:otherwise>
</c:choose>
</c:forEach>
Regards,
Justin
Justin,
spot on as usual. I had used the 'count' method of the varStatus variable in other circumstances, but hadn't realised that there were others like 'last'. Also count implies you have access to the number of items in the collection (perhaps using a 'length' function) but findResources returns an Iterator so that's not possible. You can use negation via the 'not' operator. In my case I was able to simply the logic, replacing the <c:choose> with an <c:if> as in :-
<c:if test="${not status.last}">do something with the last item</c:if>
Thanks for the pointer
Fraser
Views
Replies
Total Likes
Views
Likes
Replies