0

I have 2 Questions:

1) By using the Page name i did Xpath Query like this:

//element(*, cq:Page)
[
fn:name() = 'hello-world ' 
]

and i am able to find the page too (tried in crxde Lite and its finding the correct page). how to Convert the QueryResult to Page type?

2)After converting the result to Page type, how to retrieve a particular node under jcr:content of this Page?

I am new to CQ5, so i dont have good knowledge on existing API's provided for this. any help or pointers are much appreciated.

Kums
  • 25
  • 3
  • 7

2 Answers2

2

QueryResult wraps a collection of Nodes. Both these classes are part of the JCR API. On the other hand, Page is a part of the CQ API. There is no direct way to transform a Node into a Page even if both objects represent the same physical thing. The only thing you can do is to take the Node path and retrieve a Page using this path:

PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
QueryResult result = ...;

NodeIterator nodes = result.getNodes();
while (nodes.hasNext()) {
    Node node = nodes.nextNode();
    Page page = pageManager.getPage(node.getPath());
    // do anything you want with the Page
}

About the second question - you may use Page#getContentResource(String) method to take any Resource from the page jcr:content, like:

Resource resource = page.getContentResource("parsys/text");

Resource is a part of the Sling API. You may cast it to Node using adaptTo() method:

Node node = resource.adaptTo(Node.class);

However it's a good idea to learn how to use the Sling API. For starters, you may read resource/node properties by adapting the resource to the ValueMap class:

ValueMap map = resource.adaptTo(ValueMap.class);
map.get("myProperty", ""); // the second argument is the default value
Tomek Rękawek
  • 9,204
  • 2
  • 27
  • 43
  • Thanks a lot Tomek!!! It really helped in understanding the Basic stuff.. this should work :) – Kums Jun 27 '14 at 06:38
-2

If you are asking about JSP code, include file global.jsp and access it via properties (implicit object):

String title = properties.get("title", ""); // title = node name