This simplest method to accomplish this is to include the /libs/foundation/global.jsp
and just use the properties
object already in scope ${properties.foo}
.
Include the global.jsp at the top of the component jsp like so:
<%@include file="/libs/foundation/global.jsp"%>
As the comments in the file indicate, it basically registers the Sling (sling), CQ (cq), and JSTL (c,fmt,fn) taglib namespaces for use in the JSP.
Then, with the help of the cq:defineObjects
taglib, it brings many helpful objects into scope.
<cq:defineObjects />
Here is the list:
@param slingRequest SlingHttpServletRequest
@param slingResponse SlingHttpServletResponse
@param resource the current resource
@param currentNode the current node
@param log default logger
@param sling sling script helper
@param componentContext component context of this request
@param editContext edit context of this request
@param properties properties of the addressed resource (aka "localstruct")
@param pageManager page manager
@param currentPage containing page addressed by the request (aka "actpage")
@param resourcePage containing page of the addressed resource (aka "myPage")
@param pageProperties properties of the containing page
@param component current CQ5 component
@param designer designer
@param currentDesign design of the addressed resource (aka "actdesign")
@param resourceDesign design of the addressed resource (aka "myDesign")
@param currentStyle style of the addressed resource (aka "actstyle")
This means that by simply using the cq:defineObjects taglib you already have access to the properties ValueMap through JSP Expression Language (EL). No additional conversion is required to access the properties within the JSP.
<c:out value="${properties.foo}" />
To access the properties within your own Java taglib or bean you would just pass the appropriate object to your code using the standard JSTL tags. You could pass the whole request, the current resource, or just the properties. Passing the whole request gives your Java code access to the current resource and all the objects created by the cq:defineObjects taglib including the properties ValueMap.
In JSP:
<jsp:useBean id="mybean" scope="request" class="com.my.impl.TheBean">
<jsp:setProperty name="mybean" property="slingRequest" value="${slingRequest}"/>
<jsp:setProperty name="mybean" property="resource" value="${resource}"/>
<jsp:setProperty name="mybean" property="properties" value="${properties}"/>
</jsp:useBean>
In bean:
public void setSlingRequest(final SlingHttpServletRequest slingRequest) {
this.slingRequest = slingRequest;
// Use the one created by cq:defineObjects
this.properties = (ValueMap)this.slingRequest.getAttribute("properties");
// OR adapt the resource
this.properties = this.slingRequest.getResource().adaptTo(ValueMap.class);
}
public void setResource(final Resource resource) {
this.resource = resource;
this.properties = this.resource.adaptTo(ValueMap.class);
}
public void setProperties(final ValueMap properties) {
this.properties = properties;
}