The Apache Sling JSP Scripting Engine is implemented by the org.apache.sling.scripting.jsp
bundle, based on the Jasper 2 JSP engine.
On top of that Apache Sling also provides its own JSP Taglib, implemented by the org.apache.sling.scripting.jsp.taglib
bundle.
The Sling Scripting JSP Taglib supports the use of Sling as an application in JSP pages. The Sling Taglib provides the ability to invoke JSP scripts, include Resources and interact with the Sling Repository, all with JSP tags and Expression Language (EL) functions.
Using the Sling Taglib in a JSP page is as simple as including the Taglib include in your JSP, with the correct URI for the version of the Sling Taglib installed.
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling" %>
Generally, the prefix to use is sling
. Often applications include a global JSP file which includes the Sling Taglib and sets up all of the application variables and methods.
The Sling Taglib does not attempt to reproduce the functionality of other Tag Libraries, such as JSTL; additional Tag Libraries may be required to fully leverage the Sling Taglib.
There have been a number of releases of the Sling Taglibs, including versions with different URIs.
Taglib Version | Bundle Version | URI |
---|---|---|
1.0 | 2.0.6 | http://sling.apache.org/taglibs/sling/1.0 |
1.1 | 2.1.0 | http://sling.apache.org/taglibs/sling/1.1 |
1.2 | 2.1.8 | http://sling.apache.org/taglibs/sling/1.2 |
1.3 | 2.2.0 | http://sling.apache.org/taglibs/sling |
All releases from 1.3 onward are expected to use the URI http://sling.apache.org/taglibs/sling
to ensure ease of upgrading to newer versions of the Taglib.
The Sling Taglib includes a number of Expression Language Functions which can be used to access the repository.
Adapts an Adaptable to another class.
java.lang.Object
org.apache.sling.api.adapter.Adaptable
- The object to adaptjava.lang.String
- The name of the class to which to adapt the adaptableExample Usage
<c:set var="myProperties" value="${sling:adaptTo(resource,'org.apache.sling.api.resource.ValueMap')}" />
Writes properly Cross Site Scripting (XSS) encoded text to the response using the OWASP ESAPI. Supports a number of encoding modes.
java.util.String
- An encoded textjava.lang.String
- The text to encodejava.lang.String
- The encoding mode, one of HTML, HTML_ATTR, XML, XML_ATTR, JSExample Usage
${sling:encode('<script>alert("Bad Stuff!");</script>','HTML')}
Searches for resources using the given query formulated in the given language.
java.util.Iterator
- An Iterator of Resource objects matching the query.org.apache.sling.api.resource.ResourceResolver
- The Resource Resolver to use for the query.java.lang.String
- The query string to use to find the resources.java.lang.String
- The language in which the query is formulated.Example Usage
<c:forEach var="found" items="${sling:findResources(resourceResolver,'/jcr:root//*[jcr:contains(., 'Sling')] order by @jcr:score','xpath')">
<li>${found.path}</li>
</c:forEach>
Method for retrieving an absolute parent resource.
org.apache.sling.api.resource.Resource
- The parent resource at the specified levelorg.apache.sling.api.resource.Resource
- The current resourcejava.lang.String
- The absolute level for the parent resource to retrieveExample Usage
<c:set var="content" value="${sling:getAbsoluteParent(resource,'2')}" />
Function for retrieving all of the parent resources of a specified resource, returning them in hierarchy order.
java.lang.Iterator
- an iterator of the parent resources in orderorg.apache.sling.api.resource.Resource
- The current resource for which to retrieve the parentsjava.lang.String
- The depth at which to start, for example given a path of: /content/page1/page2/page3 and a start depth of 3, the parents page2/page3 would be returnedExample Usage
<c:set var="parents" value="${sling:getParents(resource,'2')}" />
<c:forEach var="parent" items="${parents}">
<div>${parent.path}</div>
</c:forEach>
Gets the resource at the relative path to the provided resource.
org.apache.sling.api.resource.Resource
- The resource at the relative path.org.apache.sling.api.resource.Resource
- The resource relative to which to find the path.java.lang.String
- The relative path at which to find the resource.Example Usage
<c:set var="content" value="${sling:getRelativeResource(resource,'jcr:content')}" />
Method allow for the retrieval of resources.
org.apache.sling.api.resource.Resource
- The resource at the path.org.apache.sling.api.resource.ResourceResolver
- The current resource resolver.java.lang.String
- The path at which to find the resource.Example Usage
<c:set var="content" value="${sling:getResource(resourceResolver,'/content')}" />
Gets the value of the specified key from the ValueMap and either coerses the value into the specified type or uses the specified type as a default depending on the parameter passed in.
If the third parameter is a class, the resulting value will be coersed into the class, otherwise, the third parameter is used as the default when retrieving the value from the ValueMap
.
java.lang.Object
- The value.org.apache.sling.api.resource.ValueMap
- The ValueMap from which to retrieve the value.java.lang.String
- The key for the value to retrievejava.lang.Object
- Either the default value or the class to which to coerce the value.Example Usage
<c:set var="content" value="${sling:getValue(properties,'jcr:title',resource.name)}" />
Return true if the specified resource has child resources.
java.lang.Boolean
- True if there are child resource of the specified resourceorg.apache.sling.api.resource.Resource
- The resource of which to check for children.Example Usage
<c:if test="${sling:hasChildren(resource)">
<h1>Do Something</h1>
</c:if>
Method for allowing the invocation of the Sling Resource listChildren method.
java.util.Iterator
- The children of the resource.org.apache.sling.api.resource.Resource
- The resource of which to list the children.Example Usage
<c:forEach var="child" items="${sling:listChildren(resource)">
<li>${child.path}</li>
</c:forEach>
The Sling Taglib includes a number of Tags which can be used to access the repository, handle the inclusion of scripts and manage requests.
Adapts adaptables to objects of other types.
Example Usage
<sling:adaptTo adaptable="${resource}" adaptTo="org.apache.sling.api.resource.ValueMap" var="myProps" />
Execute a script.
Example Usage
<sling:call script="myscript.jsp" />
Defines regularly used scripting variables. By default the following scripting variables are defined through this tag:
See also Scripting variables in CMS
Example Usage
<sling:defineObjects />
Writes properly Cross Site Scripting (XSS) encoded text to the response using the OWASP ESAPI. Supports a number of encoding modes.
Example Usage
<sling:encode value="<script>alert('Bad Stuff!');</script>" mode="HTML" />
Evaluates a script invocation and includes the result in the current page.
Example Usage
<sling:eval script="myscript.jsp" />
Tag for searching for resources using the given query formulated in the given language.
Example Usage
<sling:findResources query="/jcr:root//*[jcr:contains(., 'Sling')] order by @jcr:score" language="xpath" var="resources" />
Forwards a request to a resource rendering the current page
Example Usage
<sling:forward path="/content/aresource" resourceType="myapp/components/display" />
Retrieves Context-Aware Configuration resource for a specified resource, bucket and name.
Example Usage
<sling:getCAConfigResource resource="${resource}" bucket="site" name="templates" var="config" />
Retrieves Context-Aware Configuration resources for a specified resource, bucket and name.
Example Usage
<sling:getCAConfigResources resource="${resource}" bucket="site" name="templates" var="config" />
Retrieves the parent of the resource or the absolute parent at the level if specified.
Example Usage
<sling:getParent resource="${resource}" level="2" var="parent" />
Retrieves all of the parent resources of a specified resource, returning them in hierarchy order.
Example Usage
<sling:getProperties properties="${properties}" key="jcr:title" defaultValue="${resource.name}" var="title" />
Retrieves resources based on either an absolute path or a relative path and a base resource.
Example Usage
<sling:getResource base="${resource}" path="jcr:content" var="content" />
Includes a resource rendering into the current page.
Example Usage
<sling:include path="/content/aresource" resourceType="myapp/components/display" />
Lists the children of a Sling Resource.
Example Usage
<sling:listChildren resource="${resource}" var="children" />