gpt4 book ai didi

java - AEM 6.1 : Get all 'parsys' and 'iparsys' components of the page

转载 作者:行者123 更新时间:2023-12-02 02:07:23 25 4
gpt4 key购买 nike

我有一个页面路径(例如/content/my-site/en/cars),并且我需要此页面上以 java 代码表示的所有“parsys”和“iparsys”组件的列表。有什么方法可以做到吗?感谢您的帮助。

最佳答案

我假设您正在尝试 Sling modelWCMUsePOJO读取页面的内部节点。这里是技巧:

  1. 如果您不知道存在多少个 parsys 节点:这不是理想情况,因为页面渲染脚本规定了所有包含的 parsys 和 iparsys。但以防万一,您将像这样运行 sling:resourceType 查询:
Iterator<Resource> parsysResources = resourceResolver.findResources("/jcr:root/content/my-site/en/cars//*[sling:resourceType='foundation/components/parsys']", Query.XPATH);
Iterator<Resource> iparsysResources = resourceResolver.findResources("/jcr:root/content/my-site/en/cars//*[sling:resourceType='foundation/components/iparsys']", Query.XPATH);
  • 类似的查询,但使用查询构建器(推荐):建议使用查询构建器 API,以提高 future 的可读性和可扩展性。
  • List<Resource> parsysIpaysysResources = new ArrayList<>();
    Map<String, String> predicateMap = new HashMap<>();
    predicateMap.put("path", "/content/my-site/en/cars");
    predicateMap.put("1_property", "sling:resourceType");
    predicateMap.put("1_property.value", "foundation/components/parsys");
    predicateMap.put("2_property", "sling:resourceType");
    predicateMap.put("2_property.value", "foundation/components/iparsys");
    predicateMap.put("p.limit", "-1");

    QueryBuilder queryBuilder = resourceResolver.adaptTo(QueryBuilder.class);
    Session session = resourceResolver.adaptTo(Session.class);
    com.day.cq.search.Query query = queryBuilder.createQuery(PredicateGroup.create(predicateMap), session);
    SearchResult result = query.getResult();
    Iterator<Resource> resources = result.getResources();
    while (resources.hasNext()) {
    parsysIpaysysResources.add(resources.next());
    }
  • 如果已知 parsys 节点是页面内容的直接子节点,则与查询相比,listChildren 的成本会更低。
  • Page pageContent = pageManager.getContainingPage("/content/my-site/en/cars");
    Iterator<Resource> children = pageContent.getContentResource().listChildren();
    while(children != null && children.hasNext()) {
    Resource child = children.next();
    if(child.isResourceType("foundation/components/parsys") || child.isResourceType("foundation/components/iparsys")) {
    // do something
    }
    }
  • 如果知道内部 parsys 的节点名称,则可以利用 JCR API
  • Page pageContent = pageManager.getContainingPage("/content/my-site/en/cars");
    Node pageContentNode = pageContent.adaptTo(Node.class);
    try {
    NodeIterator nodeIter = pageContentNode.getNodes("parsys*");
    // iterate nodes
    } catch (RepositoryException e) {
    e.printStackTrace();
    }

    关于java - AEM 6.1 : Get all 'parsys' and 'iparsys' components of the page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50564967/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com