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>
${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 adopt 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:
Access Property of a Specific Resource Path
Once you have a Resource, you can adopt 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.
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>
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'
[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
Basic CQ5 Code Examples
Getting Resources and Properties in Sling
CQ5 coding patterns: Sling vs JCR (part 1)
Querying and Searching using JCR
No comments:
Post a Comment
If you have any doubts or questions, please let us know.