Usando c:forEach sobre sling:listChildren

Tengo el siguiente código:

<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, esta 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.

Encontré el ResourceIteratorDecorator class y se extiende Iterator<Resource>, so the class itself should not be a problem. Is c:forEach known to have problems with iterator objects?

preguntado el 09 de septiembre de 13 a las 22:09

1 Respuestas

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 sin al ${fn:length(children)}

Respondido el 13 de Septiembre de 13 a las 13:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.