cq get currentpage的jcr:content / image node property-> fileReference

时间:2014-07-18 12:48:25

标签: iterator nodes cq5 jcr sling

我正在处理一块cq组件,我需要的是检索图像文件引用路径并在页面上渲染图像。

例如,这是我的页面结构,如图所示。 enter image description here

说我当前在索引页面上,我知道如何检索索引页面的子页面并获取其jcr:content的值图,并从jcr获取所有属性:content ...

但我不知道如何检索它的jcr:content / image node->图像,以及如何检索它的文件引用属性......

这是我的代码,它崩溃了......

<%
     boolean includeTitle = properties.get("includeTitle", false);
     boolean includeImage = properties.get("includeImage", false);
     boolean includeSubTitle = properties.get("includeSubTitle", false);
     boolean includeDescription = properties.get("includeDescription", false);
     String type = currentStyle.get("type", "plain");
%>


<%
    Iterator<Page> currentPageChildren = currentPage.listChildren();
    while(currentPageChildren.hasNext()){
        Page childPage = currentPageChildren.next();
        ValueMap childPageProperties = childPage.getProperties();

        //trying to retrieve the image node
        Node imageNode = childPage.getContentResource("image").adaptTo(Node.class);

        String childPageTitle = childPageProperties.get("jcr:title", String.class);
        String childPageSubTitle = childPageProperties.get("subtitle", String.class);
        String childPageDescription = childPageProperties.get("jcr:description", String.class);
        %>
        <div>
        <% if (includeTitle) { %>
               <p><%= childPageTitle%></p>
        <% }

        if (includeSubTitle) { %>
               <p><%= childPageSubTitle%></p>
        <% }

        if (includeDescription) { %>
               <p><%= childPageDescription%></p>
        <% } %>

        //test to print image's filereference path in string on page
        <p><%=imageNode.getProperty("fileReference") %></p>

        </div>
        <%
    }
%>

请帮我一些代码示例,thx

1 个答案:

答案 0 :(得分:2)

我建议使用Image类来处理空检查。我认为在您的代码中,您偶然发现了imageNode为空或没有属性fileReference的网页。

代码段:

String fileReference = "";
Resource imgRes = childPage.getContentResource("image");
if (imgRes != null) {
    Image image = new Image(imgRes);
    fileReference = image.getFileReference();
}