New from Apache Sling: Resource Access Tags
Recently, I had the chance to release a new version of the Sling JSP Taglib. This new version includes a large number of features and improvments which I hope will help developers in Sling-based platforms, including Adobe CQ, develop cleaner and more reusable JSP scripts.
What’s new? Well, the JSP Taglib allows for access to the Sling Repository without requring any Java code. With the previous version of the Sling Taglib, if you wanted to the get the property at the path jcr:content/jcr:title for all of the children of a particular resource given a path, you could do something like this:
<ul>
<%
List<String> title = new ArrayList<String>();
Resource resource = resourceResolver.getPath(path);
for(Resource child : resource.getChildren()){
if("jcr:content".equals(child.getName())){
continue;
}
Resource content = child.getChild("jcr:content");
if(content != null){
ValueMap contentProps = content.adaptTo(ValueMap.class);
%><li><%= contentProps.get("jcr:title", child.getName()) %></li><%
}
}
%>
</ul>
As you don’t have direct access to the Sling Repository, even simple tasks require scriptlet Java code. This often ends up with a ugly mix of Java code and HTML. Instead of this, you can use the Sling Taglib to access the Sling Repository using JSP tags, as such:
<ul>
<c:forEach var="child" items="${sling:getResource(resourceResolver,path).children}">
<c:if test="${child.name != 'jcr:content'}>
<sling:getResource path="jcr:content" base="${child}" var="content" />
<sling:adaptTo adaptable="${content}" adaptTo="org.apache.sling.api.resource.ValueMap" var="properties" />
<li><c:out value="${properties['jcr:title']" default="${child.name}" /></li>
</c:if>
</c:forEach>
</ul>
This method is cleaner and more consise and allows you to retrieve simple information from the Sling Repository without requiring any scriptlet code. The new tags are also now documented including the attributes and example use of every tag and EL function.
The new version of the Sling JSP Taglib will be released with Sling 7 and presumably the next version of Adobe CQ/AEM, however you can download and install it now. It is compatible at least down to CQ 5.4 and can be downloaded from the Sling Project Downloads page.
To leverage the new Sling Resource Access Tags, simply download and install Sling JSP Taglib version 2.2.0 from the Sling project and update your Sling Taglib URI to:
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling" %>
This new Taglib URI will be used for all subsequent releases of the Sling Taglibs, so this will make your code more future-proof as well.