Skip to content

Instantly share code, notes, and snippets.

@willtonkin
Last active August 29, 2015 14:24
Show Gist options
  • Save willtonkin/9a812dd9e71eb23b4648 to your computer and use it in GitHub Desktop.
Save willtonkin/9a812dd9e71eb23b4648 to your computer and use it in GitHub Desktop.
JSP/JSTL/Sling Cheatsheet
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<h2>Important Functions</h2>
String : ${fn:replace(inputString, beforeSubstring, afterSubstring)}
String : ${fn:substring(string, beginIndex, endIndex)}
int : ${fn:length(collection or string)}
boolean : ${fn:startsWith(string, prefix)}
String[] : ${fn:split(string, delimiters)}
<h2>Important Tags</h2>
<c:redirect url="http://iwxcode.appspot.net"/>
<c:set var="test" value="Page Level Value" scope="page" />
<c:set var="test" value="Request Level Value" scope="request" />
<c:set var="test" value="Session Level Value" scope="session" />
<c:set var="test" value="Application Level Value" scope="application" />
<c:out value="${test}" />
<b>Page Level</b>
<c:out value="${pageScope.test}" />
<b>Request Level</b>
<c:out value="${requestScope.test}" />
<b>Session Level</b>
<c:out value="${sessionScope.test}" />
<b>Application Level</b>
<c:out value="${applicationScope.test}" />
<c:if test="${user.age ge 40}">
You are over the hill.
</c:if>
<c:choose>
<c:when test="${a boolean expr}">
do something
</c:when>
<c:when test="${another boolean expr}">
do something else
</c:when>
<c:otherwise>
do this when nothing else is true
</c:otherwise>
</c:choose>
<c:forEach items="${user.languages}" var="lang" varStatus="status">
<c:if test="${status.first}">Your languages:<br><ul></c:if>
<li><c:out value="${lang}"/></li>
<c:if test="${status.last}"></ul></c:if>
</c:forEach>
<c:import url="includes/header.jsp">
<c:param name="title">Hello World</c:param>
</c:import>
<!-- java.text.SimpleDateFormat -->
<fmt:formatDate value="${now}" pattern="yy-MMM-dd"/>
<fmt:formatDate value="${bday}" dateStyle="full"/>
<fmt:parseDate var="bday" pattern="MM/dd/yy" value="05/10/63"/>
<fmt:formatNumber type="currency" value="3.977">
<fmt:parseNumber var="num" type="number" pattern="#,###" value="2,447"/>
<c:out value="${num}"/>
<c:forEach var="i" begin="1" end="5">
Item <c:out value="${i}"/><p>
</c:forEach>
<h2>Access Current Resource and Its Properties</h2>
<h3>display the name of the component</h3>
${component.name}
<h3>page name</h3>
${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>
<h2>Locate a Specific Sling Resource</h2>
<%
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");
%>
<h2>Access Property of a Specific Resource Path</h2>
<%
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);
...
%>
<h2>Access HTTP Sling Request</h2>
<%
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>
<h2>Get array property</h2>
<%
String[] tabPaths = properties.get("tabPaths", String[].class);
%>
<c:if test="${not empty fn:trim(properties.title)}">
<h3><c:out value="${properties.title}" /></h3>
</c:if>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment