How to access JCR Query in JSP (AEM Component)

Posted By: Matpal Media - 8:51 AM
Following JSP explains how to access a JCR query in AEM Component. 
<%@include file="/libs/foundation/global.jsp"%><%
%><%@page session="false" %>

<%@ page  import = "java.util.HashMap,
 java.util.Map,
 javax.jcr.Node,
 javax.jcr.NodeIterator,
 javax.jcr.Session,
 org.apache.sling.api.SlingHttpServletRequest,
 javax.jcr.query.Query,
 javax.jcr.query.QueryManager,
 javax.jcr.query.QueryResult" %>

<%

String sqlStatement="";
try
{
  Session session = resourceResolver.adaptTo(Session.class);

  QueryManager queryManager = session.getWorkspace().getQueryManager();
  sqlStatement = "SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE([/content/forms/af])";
  Query query = queryManager.createQuery(sqlStatement, "JCR-SQL2");
  QueryResult result = query.execute();

  NodeIterator  nodeIter = result.getNodes();

  while ( nodeIter.hasNext() ) 
  {

     Node node = nodeIter.nextNode();
     out.print("<p>"+node.getName()+"</p>");
  }
}

catch(Exception e) 
{
out.print(e);
}
%>

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.