May 10, 2020
Estimated Post Reading Time ~

Tag Libraries in CQ5/AEM

CQ Tag Library
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %>

The CQ tag library contains helpful functions.

To use the CQ Tag Library in your script, the script must start with the following code:
When you develop the jsp script of a AEM component, it is recommended to include following code at the top of the script:
Code samples are intended for illustration purposes only.
It declares the sling, cq and jstl taglibs and exposes the regularly used scripting objects defined by the tag. This shortens and simplifies the jsp code of your component.

The tag is a convenience tag that outputs component text in a JSP.
It has the following optional attributes:

property: Name of the property to use. The name is relative to the current resource.

Value: Value to use for output. If this attribute is present, it overwrites the use of the property attribute.

OldValue: Value to use for diff output. If this attribute is present, it overwrites the use of the property attribute.

EscapeXml: Defines whether the characters , &, ‘ and ” in the resulting string should be converted to their corresponding character entity codes. Default value is false. Note that the escaping is applied after the optional formatting.

Format: Optional java.text.Format to use for formatting the text.

NoDiff: Suppresses the calculation of a diff output, even if a diff info is present.

TagClass: CSS class name of an element that will surround a non-empty output. If empty, no element is added.

TagName: Name of the element that will surround a non-empty output. It defaults to DIV.

Placeholder: Default value to use for null or empty text in edit mode, i.e. the placeholder. Please note that the default check is performed after the optional formatting and escaping, i.e. it is written as-is to the output. It defaults to:

default: Default value to use for null or empty text. Note that the default check is performed after the optional formatting and escaping i.e. it is written as-is to the output.

Some examples how the tag can be used in a JSP:

<cq:text property="jcr:title" tagName="h2"/>
<cq:text property="jcr:description" tagName="p"/>

<cq:text value="<%= listItem.getTitle() %>" tagName="h4" placeholder="" />
<cq:text value="<%= listItem.getDescription() %>" tagName="p" placeholder=""/>

<cq:text property="jcr:title" value="<%= title %>" tagName="h3"/><%
} else if (type.equals("link")) {
%><cq:text property="jcr:title" value="<%= "\u00bb " + title %>"tagName="p" tagClass="link"/><%
} else if (type.equals("extralarge")) {
%><cq:text property="jcr:title" value="<%= title %>" tagName="h1"/><%
} else {
%><cq:text property="jcr:title" value="<%= title %>" tagName="h2"/><%

<cq:text property="jcr:description" placeholder="" tagName="small"/>

<cq:text property="tableData"
escapeXml="false"
placeholder="<img src=\"/libs/cq/ui/resources/0.gif\" class=\"cq-table-placeholder\" alt=\"\">"
/>

<cq:text property="text"/>

<cq:text property="image/jcr:description" placeholder="" tagName="small"/>
<cq:text property="text" tagClass="text"/>


Code samples are intended for illustration purposes only.

The tag creates an i18n localization context and stores it in thejavax.servlet.jsp.jstl.fmt.localizationContext configuration variable.
It has the following attributes:

language
The language of the locale for which to retrieve the resource bundle.
source
The source where the locale should be taken from. It can be set to one of the following values:
static: the locale is taken from the language attribute if available, otherwise from the server default locale.
page: the locale is taken from the language of the current page or resource if available, otherwise from thelanguage attribute if available, otherwise from the server default locale.
request: the locale is taken from the request locale (request.getLocale()).
auto: the locale is taken from the language attribute if available, otherwise from the language of the current page or resource if available, otherwise from the request.
If the source attribute is not set (as it was the case until CQ 5.3):
If the language attribute is set, the source attribute defaults to static.
If the language attribute is not set, the source attribute defaults to auto.
The “content bundle” can be simply used by standard JSTL tags. The lookup of messages by keys is two-fold:
  1. First, the JCR properties of the underlying resource that is currently rendered are searched for translations. This allows you to define a simple component dialog to edit those values.
  2. If the node does not contain a property named exactly like the key, the fallback is to load a resource bundle from the sling request (SlingHttpServletRequest.getResourceBundle(Locale)). The language or locale for this bundle is defined by the language and source attributes of the tag.
The tag can be used as follows in a jsp.
For pages that define their language:

... %><cq:setContentBundle source="page"/>
<div class="error"><fmt:message key="Hello"/>
</div> ...


Code samples are intended for illustration purposes only.
For user personalized pages:

... %><cq:setContentBundle scope="request"/>
<div class="error"><fmt:message key="Hello"/>
</div> ...
Code samples are intended for illustration purposes only.

The tag includes a resource into the current page.
It has the following attributes:

flush
A boolean defining whether to flush the output before including the target.

path
The path to the resource object to be included in the current request processing. If this path is relative it is appended to the path of the current resource whose script is including the given resource. Either path and resourceType, or script must be specified.

resourceType
The resource type of the resource to be included. If the resource type is set, the path must be the exact path to a resource object: in this case, adding parameters, selectors and extensions to the path is not supported.
If the resource to be included is specified with the path attribute that cannot be resolved to a resource, the tag may create a synthetic resource object out of the path and this resource type.
Either path and resourceType, or script must be specified.

script
The jsp script to include. Either path and resourceType, or script must be specified.
ignoreComponentHierarchy
A boolean controlling whether the component hierarchy should be ignored for script resolution. If true, only the search paths are respected.

Example:
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%
%><div class="center">
<cq:include path="trail" resourceType="foundation/components/breadcrumb" />
<cq:include path="title" resourceType="foundation/components/title" />
<cq:include script="redirect.jsp"/>
<cq:include path="par" resourceType="foundation/components/parsys" />
</div>
Code samples are intended for illustration purposes only.

Should you use or to include a script?
The directive informs the JSP compiler to include a complete file into the current file. It is as if the contents of the included file were pasted directly into the original file.
With the tag, the file is included at runtime.

Should you use or ?
When developing AEM components, Adobe recommends that you use .
allows you to directly include script files by their name when using the script attribute. This takes component and resource type inheritance into account, and is often simpler than strict adherence to Sling’s script resolution using selectors and extensions.

The tag Includes a AEM html client library, which can be a js, a css or a theme library. For multiple inclusions of different types, for example js and css, this tag needs to be used multiple times in the jsp. This tag is a convenience wrapper around the com.day.cq.widget.HtmlLibraryManager service interface.
It has the following attributes:

categories
A list of comma-separated client lib categories. This will include all Javascript and CSS libraries for the given categories. The theme name is extracted from the request.
Equivalent to: com.day.cq.widget.HtmlLibraryManager#writeIncludes

theme
A list of comma-separated client lib categories. This will include all theme related libraries (both CSS and JS) for the given categories. The theme name is extracted from the request.
Equivalent to: com.day.cq.widget.HtmlLibraryManager#writeThemeInclude

js
A list of comma-separated client lib categories. This will include all Javascript libraries for the given categories.
Equivalent to: com.day.cq.widget.HtmlLibraryManager#writeJsInclude

css
A list of comma-separated client lib categories. This will include all CSS libraries for the given categories.
Equivalent to: com.day.cq.widget.HtmlLibraryManager#writeCssInclude

themed
A flag that indicates of only themed or non themed libraries should be included. If omitted, both sets are included. Only applies to pure JS or CSS includes (not for categories or theme includes).
The tag can be used as follows in a jsp:

<cq:includeClientLib categories="cq.wcm.edit" />
<cq:includeClientLib js="cq.collab.calendar, cq.security" />
<cq:includeClientLib theme="cq.collab.calendar, cq.security" />
<cq:includeClientLib css="cq.collab.calendar, cq.security" />

Code samples are intended for illustration purposes only.

The tag exposes the following, regularly used, scripting objects which can be referenced by the developer. It also exposes the objects defined by the tag.

componentContext
the current component context object of the request (com.day.cq.wcm.api.components.ComponentContext interface).

component
the current AEM component object of the current resource (com.day.cq.wcm.api.components.Component interface).

currentDesign
the current design object of the current page (com.day.cq.wcm.api.designer.Design interface).

currentPage
the current AEM WCM page object (com.day.cq.wcm.api.Page interface).

currentStyle
the current style object of the current cell (com.day.cq.wcm.api.designer.Style interface).

designer
the designer object used to access design information (com.day.cq.wcm.api.designer.Designer interface).

editContext
the edit context object of the AEM component (com.day.cq.wcm.api.components.EditContext interface).

pageManager
the page manager object for page level operations (com.day.cq.wcm.api.PageManager interface).

pageProperties
the page properties object of the current page (org.apache.sling.api.resource.ValueMap).

properties
the properties object of the current resource (org.apache.sling.api.resource.ValueMap).

resourceDesign
the design object of the resource page (com.day.cq.wcm.api.designer.Design interface).

resourcePage
the resource page object (com.day.cq.wcm.api.Page interface).
It has the following attributes:

requestName
inherited from sling

responseName
inherited from sling

resourceName
inherited from sling

nodeName
inherited from sling

logName
inherited from sling

resourceResolverName
inherited from sling

slingName
inherited from sling

componentContextName
specific to wcm

editContextName
specific to wcm

propertiesName
specific to wcm

pageManagerName
specific to wcm

currentPageName
specific to wcm

resourcePageName
specific to wcm

pagePropertiesName
specific to wcm

componentName
specific to wcm

designerName
specific to wcm

currentDesignName
specific to wcm

resourceDesignName
specific to wcm

currentStyleName
specific to wcm

Example
<%
%><%
%><%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" %><%
%><cq:defineObjects/>

The tag writes the current request URL to the JspWriter. The two tags and and may be used inside the body of this tag to modify the current request URL before it is written.
It allows you to create links to the current page with varying parameters. For example, it enables you to transform the request:
mypage.html?mode=view&query=something into mypage.html?query=something.
The use of addParam or removeParam only changes the occurrence of the given parameter, all other parameters are unaffected.
does not have any attribute.

Examples:
<a href="<cq:removeParam name="language"/></cq:requestURL>">remove filter</a>
<a title="filter results" href="<cq:addParam name="language" value="${bucket.value}"/></cq:requestURL>">${label} (${bucket.count})</a>

The tag adds a request parameter with the given name and value to the enclosing tag.
It has the following attributes:

name
name of the parameter to be added

value
value of the parameter to be added

Example:
<a title="filter results" href="<cq:addParam name="language" value="${bucket.value}"/></cq:requestURL>">${label} (${bucket.count})</a>

The tag removes a request parameter with the given name and value from the enclosing tag. If no value is provided all parameters with the given name are removed.
It has the following attributes:

name
name of the parameter to be removed

Example:
<a href="<cq:removeParam name="language"/></cq:requestURL>">remove filter</a>

Sling Tag Library
The Sling tag library contains helpful Sling functions.
When you use the Sling Tag Library in your script, the script must start with the following code:

<%@ taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>

Code samples are intended for illustration purposes only.

The tag includes a resource into the current page.
It has the following attributes:

flush
A boolean defining whether to flush the output before including the target.

resource
The resource object to be included in the current request processing. Either resource or path must be specified. If both are specified, the resource takes precedence.

path
The path to the resource object to be included in the current request processing. If this path is relative it is appended to the path of the current resource whose script is including the given resource. Either resource or path must be specified. If both are specified, the resource takes precedence.

resourceType
The resource type of the resource to be included. If the resource type is set, the path must be the exact path to a resource object: in this case, adding parameters, selectors and extensions to the path is not supported.
If the resource to be included is specified with the path attribute that cannot be resolved to a resource, the tag may create a synthetic resource object out of the path and this resource type.

replaceSelectors
When dispatching, the selectors are replaced with the value of this attribute.

addSelectors
When dispatching, the value of this attribute is added to the selectors.

Examples:
<div class="item"><sling:include path=""/></div>
<sling:include resource=""/>
<sling:include addSelectors="spool"/>
<sling:include resource="" resourceType=""/>
<sling:include resource="" resourceType=""/>
<sling:include replaceSelectors="content" />

The tag exposes the following, regularly used, scripting objects which can be referenced by the developer:

slingRequest
SlingHttpServletRequest object, providing access to the HTTP request header information – extends the standard HttpServletRequest – and provides access to Sling-specific things like resource, path info, selector, etc.

slingResponse
SlingHttpServletResponse object, providing access for the HTTP response that is created by the server. This is currently the same as the HttpServletResponse from which it extends.request
The standard JSP request object which is a pure HttpServletRequest.response
The standard JSP response object which is a pure HttpServletResponse.

resourceResolver
The current ResourceResolver object. It is the same as slingRequest.getResourceResolver()

.sling
A SlingScriptHelper object, containing convenience methods for scripts, mainly sling.include(‘/some/other/resource’) for including the responses of other resources inside this response (eg. embedding header html snippets) and sling.getService(foo.bar.Service.class) to retrieve OSGi services available in Sling (Class notation depending on scripting language).

resource
the current Resource object to handle, depending on the URL of the request. It is the same as slingRequest.getResource().

currentNode
If the current resource points to a JCR node (which is typically the case in Sling), this gives direct access to the Node object. Otherwise this object is not defined.

log
Provides an SLF4J Logger for logging to the Sling log system from within scripts, eg. log.info(“Executing my script”).
It has the following attributes:
requestName
responseName
nodeName

logName resourceResolverName
slingName

Example:
<%
%><%
%><%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %><%
%><sling:defineObjects/>
Code samples are intended for illustration purposes only.

JSTL Tag library
The JavaServer Pages Standard Tag Library contains a lot of useful and standard tags. The core, formatting and functions taglibs are defined by the /libs/foundation/global.jsp as shown in the following snippet.
Extract of /libs/foundation/global.jsp

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Code samples are intended for illustration purposes only.
After importing the /libs/foundation/global.jsp file as described before, you can use the c, fmt and fn prefixes to access to those taglibs. The official documentation of the JSTL is available at The Java EE 5 Tutorial – JavaServer Pages Standard Tag Library.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.