【问题标题】:How To Retrieve First PAR Node from Content Page如何从内容页面检索第一个 PAR 节点
【发布时间】:2013-03-14 15:38:14
【问题描述】:

我正在遍历所有子页面以显示它们的标题和链接。但是我还需要显示第一个段落节点,如果它存在的话。

例如,哇,我会从以下内容页面检索第一个 PAR 节点吗?

/content
  /foo
     /jcr:content
        /title
        /par <- need this one
        /par
        /image

我认为Page class getProperties().get() 方法会起作用,但我只看到示例返回 jcr:content 中的属性,而不是它下面的任何子节点。

    ArrayList aChildren = new ArrayList();
    String listroot = properties.get("listRoot", currentPage.getPath());

    Page rootPage = pageManager.getPage(listroot);
    if (rootPage != null) {
        Iterator<Page> children = rootPage.listChildren(new PageFilter(request));

        while (children.hasNext()) {
            Page child = children.next();

            out.println( child.getTitle() + "<br>" );
            //Output first PAR tag of this page here
        }

    }

这可以通过其他特定于 CQ 的标签来完成吗,或者这是 java 函数的工作?

【问题讨论】:

    标签: java iteration aem


    【解决方案1】:

    您必须遍历子页面的子节点。

    获取资源类型为 parsys 的第一个节点。拥有该节点后,您可以获取其路径并将其包含在当前页面中。

    Resource childResource = resourceResolver.getResource(child.getPath());
    Node childNode = childResource.adaptTo(Node.class);
    Node jcrContent = childNode.getNode("jcr:content");
    NodeIterator childrenNodes = jcrContent.getNodes();
    
    while(childrenNodes.hasNext()){
        Node next = childrenNodes.nextNode();
        String resourceType = next.getProperty("sling:resourceType").getString();
        if(resourceType.equals("foundation/components/parsys")){
            %><cq:include path="<%= next.getPath() %>" resourceType="foundation/components/parsys" /><%
            break;
        }
    }
    

    这将在当前页面上嵌入子页面上的第一个 parsys 组件。我没有对此进行测试,因此可能需要进行一些修改才能使其正常工作。

    【讨论】:

    • 类型不匹配:无法从属性转换为字符串(对于String resourceType = next.getProprty("sling:resourceType")
    • 这是题外话,但我是否能够在不使用cq:include 的情况下检索段落内容?我需要将内容传递给一个变量。
    • next.getProperty("sling:resourceType").getString()您可以传递par节点var,并根据需要访问par下包含的内容。
    • 这将返回用于此内容页面的组件页面的类型。 (-site-/components/page/popuppage)
    • 我们是否需要另一个内部循环来遍历当前jcr:content节点以下的任何内容?
    【解决方案2】:

    你也可以试试这个:

    <%@page session="false" import="com.day.cq.wcm.foundation.Paragraph,
                                    com.day.cq.wcm.foundation.ParagraphSystem"%>
    <%
    ParagraphSystem parSys = ParagraphSystem.create(resource, slingRequest);
    for (Paragraph par: parSys.paragraphs()){
    

    有了这个,你可以遍历当前资源下的 parsys 节点。

    【讨论】:

    • 如何进一步迭代子节点的这个结果?:Paragraph, path=/content/-site-/test/news/news3/jcr:content, type=-site-/components/page/popuppage, cssClass=default, column=0/0, diffInfo=[null], resource=[JcrNodeResource, type=-site-/components/page/popuppage, superType=null, path=/content/-site-/test/news/news3/jcr:content]
    • 只需在每个结果上使用adaptTo(Node.class),您可以在该对象上调用getNodes() 方法,这样您就可以使用NodeIterator 遍历parsys 的子节点。
    【解决方案3】:

    CQ5 存储库中的每个节点都可以表示为Resource。您可以使用以下代码获取资源

    //resolver being instance of org.apache.sling.api.resource.ResourceResolver
    Resource paraResource = resolver.getResource("path of the paragraph");
    

    然后你可以对资源进行操作

    【讨论】:

      【解决方案4】:

      如果您尝试将 parsys 从一页引用到另一页,我会使用开箱即用的 reference component。该组件接受您网站上任何位置的组件路径,并将其显示在您选择的页面上。

      【讨论】:

      • 要调用的页面是用户输入的,引用组件不够灵活。此外,如果给定页面不存在 PAR,则不应返回任何内容;参考在这方面是无用的,因为系统,而不是用户需要执行树搜索。
      猜你喜欢
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多