3

i am working on a carousel component called "tabbedcarousel", i need to iterate through it's children nodes to access an image property. see the pic below:

enter image description here

that "tabbedimagecarouselunselectedimage" -> "fileReference" is what i need to get.

say i m currently working on "tabbedcarousel.jsp", i have access to my currentNode, so the way i m assuming to get the deep child img property is like this:

<%


        NodeIterator tabbedCarouselChildNodes = currentNode.getNodes();
        while(tabbedCarouselChildNodes.hasNext()){
            Node parNode = tabbedCarouselChildNodes.nextNode();

            NodeIterator parChildNodes = parNode.getNodes();
            while(parChildNodes.hasNext()){
                Node tabbedCarouselItemNode = parChildNodes.nextNode();

                NodeIterator tabbedCarouselItemChildNodes = tabbedCarouselItemNode.getNodes();
                while(tabbedCarouselItemChildNodes.hasNext()){
                    Node tabImgNode = tabbedCarouselItemChildNodes.nextNode();
                    %>
                    test here s the img property : <%= tabImgNode.getProperties("fileReference").toString()%> !!!
                    <% 
            }
        }

%>

but it does not work. hopefully someone can give me some suggestions. thanks

sefirosu
  • 2,558
  • 7
  • 44
  • 69

2 Answers2

8

I've added one brace at the end and replaced tabImgNode.getProperties() with hasProperty()/getProperty() combo. Works fine on my CQ:

<%
NodeIterator tabbedCarouselChildNodes = currentNode.getNodes();
while(tabbedCarouselChildNodes.hasNext()) {
    Node parNode = tabbedCarouselChildNodes.nextNode();
    NodeIterator parChildNodes = parNode.getNodes();
    while(parChildNodes.hasNext()){
        Node tabbedCarouselItemNode = parChildNodes.nextNode();
        NodeIterator tabbedCarouselItemChildNodes = tabbedCarouselItemNode.getNodes();
        while(tabbedCarouselItemChildNodes.hasNext()){
            Node tabImgNode = tabbedCarouselItemChildNodes.nextNode();
            if (!tabImgNode.hasProperty("fileReference")) {
                continue;
            }
%>
test: <%= tabImgNode.getProperty("fileReference").getString()%>
<% 
        }
    }
}
%>

BTW, it can be refactored using Sling API:

<%
Iterator<Resource> items = resource.getChild("par").listChildren();
while (items.hasNext()) {
    Resource property = items.next().getChild("tabimageunselectedimage/fileReference");
    if (property == null) {
        continue;
    }
%>
fileReference: <%= property.adaptTo(String.class) %>
<% } %>

The third option is to use 3rd party library called SlingQuery:

<% for (Resource r : SlingQuery.$(resource).find("#tabimageunselectedimage")) { %>
  path: <%= r.adaptTo(ValueMap.class).get("fileReference") %>
<% } %>
Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43
0

Came across this because I was looking for the must effective way to retrieve jcr nodes to any depth given the start node. In your case, I will use recursive to get the desire result.

In OSGI service

public class WriteImageNode{
    private SlingHttpServletRequest slingRequest;
    private SlingScriptHelper sling;
    private ArrayList<String, String> tabImageList = new ArrayList<String, String>();
    public WriteImageNode( SlingHttpServletRequest slingRequest, SlingScriptHelper sling){
        this.slingRequest = slingRequest;
        this.sling = sling;

    }
    public ArrayList<String> getTabImageNode(String path){
        Session currentSession = null;
        SlingRepository repo ;
        if(!path.equals("")){
                  Node startNode = slingRequest.getResourceResolver().getResource(path).adaptTo(Node.class);
               try {
                   repo = sling.getService(SlingRepository.class);

                   currentSession = repo.loginAdministrative(null);

                   NodeIterator iterator = startNode.getNodes();
                   while(iterator.hasNext()){
                        Node childNode = iterator.nextNode();
                        if(childNode.hasProperty("fileReference")){
                            tabImageList.add(childNode.getProperty("fileReference").getString());
                          }
                          else{
                                 getTabImageNode(childNode.getPath());
                              }
                   }

            } catch (RepositoryException e) {
                   //do something clever with the exception

               } finally {
                   if (currentSession != null) {
                       currentSession.logout();
                   }
            } 
       }
    }
    return tabImageList;
}

In your jsp do something like this:

    <%
       request.setAttribute("tabImageList",  new WriteImageNode(slingRequest, sling).getTabImageNode(path));
    %>
<c:forEach var="imagelist" items="${tabImageList}">
   <c:out value="${imagelist}/>
</c:forEach>
olatno
  • 189
  • 2
  • 5