New from Apache Sling: Resource Access Tags

Published on by Dan KlcoPicture of Me Dan Klco

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:

	List<String> title = new ArrayList<String>();
	Resource resource = resourceResolver.getPath(path);
	for(Resource child : resource.getChildren()){
		Resource content = child.getChild("jcr:content");
		if(content != null){
			ValueMap contentProps = content.adaptTo(ValueMap.class);
			%><li><%= contentProps.get("jcr:title", child.getName()) %></li><%

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:

	<c:forEach var="child" items="${sling:getResource(resourceResolver,path).children}">
		<c:if test="${ != 'jcr:content'}>
			<sling:getResource path="jcr:content" base="${child}" var="content" />
			<sling:adaptTo adaptable="${content}" adaptTo="" var="properties" />
			<li><c:out value="${properties['jcr:title']" default="${}" /></li>

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="" %>

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.


comments powered by Disqus