搜索深节点

时间:2013-09-03 11:29:48

标签: java cq5 jcr

假设我有以下结构

myPage
jcr:content
  ->parContent
      ->myComponent

如果知道myComponent,找到myPage path的优雅方式是什么?

我使用以下内容:

private Node myNode(String myPagePath){
    ResourceResolver resourceResolver = request.getResourceResolver();
    Resource resource = resourceResolver.getResource(myPagePath);
    final Node currentNode = resource.adaptTo(Node.class);

    return currentNode.getNode("jcr:content").getNode("parContent").getNode("myComponent");
}

当我们有很多节点时,我找不到getNode()。getNode()....非常特别。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

getNode(String)方法接受相对路径,因此虽然子名称是相对路径并且经常使用此方法,但您也可以构建相对路径:

Node result = currentNode.getNode("jcr:content/parContent/myComponent");

正如您所看到的,它非常简单。当然,JCR中的路径非常强大;请参阅JCR 2.0规范中的Section 3.4,以获得绝对和相对路径中允许的完整定义。

顺便说一句,如果您有绝对路径,则必须直接从Session查找节点:

Node result = mySession.getNode("/some/absolute/path/to/a/node");

答案 1 :(得分:0)

我不鼓励在JSP的开发团队中使用Node API。相反,ValueMap接口允许访问任何属性,包括相对于页面的路径。它们不会抛出异常,并且如果找不到则允许默认值。

您可以将资源转换为ValueMap(您可以将节点,页面转换为资源,与节点一样容易):

ValueMap vm = resource.adaptTo(ValueMap.class);
相关问题