我有一个指向页面的路径(例如/ content / my-site / en / cars),我需要一个包含所有' parsys'的列表。和' iparsys'在java代码中显示在此页面上的组件。有什么办法吗?谢谢你的帮助。
答案 0 :(得分:2)
我假设您正在尝试Sling model或WCMUsePOJO来读取页面的内部节点。这里的技巧:
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);
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());
}
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
}
}
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();
}