Featured

Thursday 13 February 2014

How to get node properties in jsp in cq5

Here is the procedure to get node properties in a jsp in cq5


<%@page session="false"%>
<%@ page import="java.util.Locale,
java.util.ResourceBundle,
javax.jcr.NodeIterator,
javax.jcr.Session,
javax.jcr.Node, javax.jcr.Value, javax.jcr.NodeIterator,
com.day.cq.commons.jcr.*" %>
<%@include file="/libs/foundation/global.jsp" %>

<%
javax.jcr.Session ses = resourceResolver.adaptTo(Session.class);
        //If you want to save anything to the node this session object (ses)is required.
String nodePath="/content/****";
Resource resources = slingRequest.getResourceResolver().getResource(nodePath);
         //Creating Node object using adaptTo mehtod.
Node rootNode = resources.adaptTo(Node.class);

*****Here is the node "rootnode", and now we can explore subnodes by using the root node and their propeties.

Comments are most welcome!!!
Thanks,
SonyCharan

4 comments:

Anonymous said...

Thanks a lot buddy

Anonymous said...

thank you very much for sharing this information. It helped me accomplish a major task in my project.

Sabya said...

Nice article. I think , it will always be good to use a Sling specific interface(Resource) rather than using a low level JCR interface (Node). Thus, you delegate the responsibility to Sling to deal with repository rather than doing urself.
Getting node properties will also be easier in that case.

ValueMap vm = resource.adaptTo(ValueMap.class);
vm.get("propertyName", String.class);

Unknown said...

Thanks Sabya. Accepted .