Application Development, Product to Market
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use
  • Home
  • Notes
  • Projects
  • Resources
    • Discovery Log
    • Books I Read
    • Blogs I Visit
    • Tools I Use

CQ5 Code Samples

8/27/2013

2 Comments

 
Coding for CQ/WEM can be quite confusing sometimes, as confusing as it is whether it's CQ or WEM. This post is about basic WEM Code samples (in JSP) using CQ scripting objects on top of JSP implicit objects. First of all, let me be clear, since Adobe acquired Day Software, the name of Day CQ (Comuniqué) had changed to WEM (Web Experience Manager), thus CQ and WEM are interchangeable.
When you develop the jsp script of a AEM component, most of the cases you'd include following code at the top of the JSP script:
<%@include file="/libs/foundation/global.jsp"%>
It declares and exposes the regularly used scripting objects defined by the sling taglibs, cq taglibs and jstl taglibs at your disposal in JSP. Assuming you have the above line in your .jsp, the following code samples show you some very basic tasks that you usually have to accomplish.

Access Current Resource and Its Properties

Most of the time, you'd access the current page, current component, current whatever, etc. The global.jsp brings many exposed variable to you to access current resource via EL expression:
<h1>display the name of the component</h2>
${component.name}
 
<h2>page name</h2>
${currentPage.name}


<h3>resource page name</h3>
${resourcePage.name}


<h3>custom page property</h3>
${pageProperties.propertyName}               // 1
${currentPage.properties.customProperty}     // same as above
 
<h3>property set via the Edit dialog</h3>
${properties.myProperty}
 
<h3>property set via the Edit dialog</h3>
${properties["myProperty"]}
 
<h3>print a property via cq:text</h3>
<cq:text property="jcr:subtitle" tagname="h3" placeholder="default placeholder">
 
<h3>assigned property value to a var</h3>
<c:set var="newvar">
  <cq:text property="jcr:subtitle">
</cq:text></c:set>
${newvar}
</cq:text>
[1] "propertyName" is the property name of the jcr:content node of the current page.

Locate a Specific Sling Resource

If you'd like to access Resource other than current node, component, page, etc. but instead need to access property of a non-current resource. How would you code that?

CQ is both JCR and Sling. JCR are about Node and Property, whereas Sling assumes everything is a Resource which is adaptable to other classes. To find Resource(s), use ResourceResolver:
<%
Resource myResource = resourceResolver.getResource("/content/dam/campaign");

String query = "/jcr:root/etc/workflow/instances///jcr:root/etc/replication/agents.author//*[@transportUri]"; // 1
Iterator<Resource> myResources = resourceResolver.findResources(query, "xpath"); 
%>
[1] The xpath syntax to match resources that has property named 'transporUri'.

Access Property of a Specific Resource Path

Once you have a Resource, you can adapt a Resource to a Page,  adapt a Resource to a ValueMap, a Node, or an InputStream, etc., then access the ValueMap for the property value. In your JSP file, do this:
<%
Page page = myResource.adaptTo(Page.class);
String title1 = page.getTitle();
ValueMap myProperties = myResource.adaptTo(ValueMap.class); // 1
String title2 = myProperties.get("title", "defaultValue");

Node node = myResource.adaptTo(Node.class);                 // 2
InputStream inputStream = myResource.adaptTo(InputStream.class);
...
%>
[1] The ValueMap is an easy way to access properties of a resource.
[2] If you ever need to deal with JCR node directly.

Access HTTP Sling Request

From a slingRequest, you can get the URL in string presentation, or get the path information about the sling resource's path, selector, extension, suffix, etc.
<%
String url = slingRequest.getRequestURL().toString(); // 1
RequestPathInfo pathInfo = slingRequest.getRequestPathInfo(); // 2 
%>


<c:foreach var="item" varstatus="status" items="${slingRequest.requestPathInfo.selectors}">
${status.count}: ${item} <br>
</c:foreach>
[1] http://localhost:4502/content/public/estate/estate-administration/checklist-the-executor-s-role.pdf
[2] e.g., path = '/content/public/estate/estate-administration/checklist-the-executor-s-role/jcr:content',  selectorString = 'download', extension='pdf', suffix='null'

References

  • Basic CQ5 Code Examples
  • Getting Resources and Properties in Sling
  • CQ5 coding patterns: Sling vs JCR (part 1)
  • Querying and Searching using JCR
2 Comments
Ruen link
10/8/2014 10:14:18 pm

Thanks for this :) handy guide

Reply
Omar
8/26/2015 01:34:37 am

Hi Hsufeng,

do you know why I can take the parent of the current page with:

<%= currentPage.getParent() %>

while I obtain NullPointerException using JSTL?

${currentPage.parent}

Either works for getName(), instead.

Reply



Leave a Reply.

    Categories

    All
    Algorithm
    Concurrency
    CQ
    Data Structure
    Design Pattern
    Developer Tool
    Dynamic Programming
    Entrepreneur
    Functional Programming
    IDE
    Java
    JMX
    Marketing
    Marklogic
    Memory
    OSGI
    Performance
    Product
    Product Management
    Security
    Services
    Sling
    Social Media Programming
    Software Development

    Feed Widget

    Archives

    May 2020
    March 2020
    April 2018
    March 2018
    February 2018
    December 2017
    March 2017
    November 2016
    June 2016
    May 2016
    April 2016
    October 2015
    September 2015
    August 2015
    September 2014
    July 2014
    June 2014
    May 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    June 2013

    RSS Feed

in loving memory of my mother  and my 4th aunt
Photos used under Creative Commons from net_efekt, schani, visnup, Dan Zen, gadl, bobbigmac, Susana López-Urrutia, jwalsh, Philippe Put, michael pollak, oskay, Creative Tools, Violentz, Kyknoord, mobilyazilar