Sunday, April 10, 2011

Queries in Sling

Here is how I use JCR queries in my .esp pages.
The sample below lists all child nodes of the current node sorted by price property.


<%
var sql = "select * from [nt:base] where ischildnode('" + currentNode + "') order by price asc";
var query = currentSession.workspace.queryManager.createQuery(sql, Packages.javax.jcr.query.Query.JCR_SQL2);
var result = query.execute();
var iter = result.nodes;
while (iter.hasNext()) {
var node = iter.nextNode();
%>

// render each node from the result

<% } %>


Notice that when a Node is converted to string its path is used.
Also here you can see how Packages object is used to access static members of Java classes. This is a Rhino feature. You can read more about it here.

No comments:

Post a Comment