Search code examples
jspsling

Apache Sling and scriptlets


I'm new to Apache Sling and I see in all the examples that it uses JSP scriptlets to render the pages.

Seriously? Is there a way to use normal java code instead of a technique considered obsolete, and discouraged by Sun itself more than 10 years ago due to all the disadvantages it presents?

Or maybe there is an obscure reason where the use of scriptlets is the best choice for the implementation and I'm not aware of that.


Solution

  • If you don't like scriptlets you can also use taglibs and EL. For a quick overview, see the latest version of the TLD file in the Sling SVN repository. You can of course use Javascript, Groovy, Ruby or any other scripting language supported by Sling.

    As for the taglibs, to quote SLING-2648: Add Resource Access Tags , you can use them to do things like

    <c:set var="currentResource" value="${sling:getResource(resourceResolver,'/etc')}" />
    <c:forEach var="child" items="${sling:listChildResources(currentResource)}">
        <c:if test="${child.name != 'jcr:content'}">
            <sling:getResource var="pageContent" path="${child.path}/jcr:content" />
            <sling:adapt adaptable="${pageContent}" adaptTo="org.apache.sling.api.resource.ValueMap" var="childProperties" />
            <sling:getProperty key="jcr:title" defaultValue="${child.name}" var="title" properties="${childProperties}" />
            <li><a href="${child.path}.html">Title: ${title}</a></li>
        </c:if>
    </c:forEach>