AEM 6.1:获取所有' parsys'和' iparsys'页面的组件

时间:2018-05-28 11:07:03

标签: java aem cq5

我有一个指向页面的路径(例如/ content / my-site / en / cars),我需要一个包含所有' parsys'的列表。和' iparsys'在java代码中显示在此页面上的组件。有什么办法吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

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

  1. 如果您不知道有多少个parsys节点:这不是一个理想的情况,因为页面呈现脚本指示所有包含的parsys和iparsys。但是,只要加入,你就会运行sling:resourceType的查询,如下所示:
  2. 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);
    
    1. 类似查询,但使用查询生成器(推荐):建议使用查询生成器API以提高可读性,并在将来进行扩展。
    2. 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());
      }
      
      1. 如果已知parsys节点是页面内容的直接子节点,则listChildren与查询相比会更便宜。
      2. 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
                    }
                }
        
        1. 如果已知内部parsys的节点名称,则可以利用JCR API
        2. 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();
              }