Question

I have the following code:

<c:set var="res" value="${sling:getResource(resourceResolver, currentPage.path)}" />
<sling:listChildren resource="${res}" var="children" />
${children} length ${fn:length(children)}
<c:forEach var="child" items="${children}">
     <li>here${child.path}</li>
</c:forEach>

My goal is to spit out the three children of my current page. When I print out ${children}, I get an object reference to an iterator (org.apache.sling.resourceresolver.impl.helper.ResourceIteratorDecorator@3b93b521) and the length returns three. The problem is that I cannot get into the loop. According to the sling taglibs docs, this forEach should be able to handle the iterator object. Is there something I'm missing? The findResources function also returns a ResourceIteratorDecorator when it should be a plain java.util.Iterator.

I did find the ResourceIteratorDecorator class and it extends Iterator<Resource>, so the class itself should not be a problem. Is c:forEach known to have problems with iterator objects?

Was it helpful?

Solution

Try taking out length check and allowing the forEach to run.

For the length function to work out how many items are included in the iterator it must call next() on the iterator until it gets to the end. There is no way to rewind an iterator, so by the time you get to your forEach there are no items left.

If you need to know the length you can use a varStatus object:

<c:forEach var="child" items="${children}" varStatus="status">
     <li>${status.index} here${child.path}</li>
</c:forEach>

You are using a newer version of Sling than me, but this works for me if I feed the results of ResourceResolver.listChildren into a c:forEach without the ${fn:length(children)}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top