Custom Tag Libs for Adobe CQ

Follow the below steps for creating custom Tab libraries:

Step 1: Write a java Class with the method  you want as a tag lib.

I’m providing an example java class here:

/**

*

*/

package com.company.cq.taglibs;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.tagext.DynamicAttributes;

import javax.servlet.jsp.tagext.TagSupport;

/**

* @author rmengji

*

*/

public class ReusableTagLibs extends TagSupport implements DynamicAttributes {

@Override

public int doStartTag() throws JspException {

try {

JspWriter out = pageContext.getOut();

out.print(getLabel(“”,””));

}catch (Exception e) {

// TODO: handle exception

}

return SKIP_BODY;

}

private String getLabel(String prop, String value) {

return “hello”;

}

public void setDynamicAttribute(String uri, String localName, Object value) {

}

}

Step 2: Write a tld file for your tab library

The path of the tld file may be in  resource/META-INF folder of your src folder. If META-INF folder is not present then create it.

The tld file will look like this:

<?xml version=”1.0″ encoding=”UTF-8″?>

<taglib>

<tlibversion>1.0</tlibversion>

<jspversion>1.1</jspversion>

<shortname>locale</shortname>

<info>Taglib for CQ localization</info>

<tag>

<name>label</name>

<tagclass>com.company.cq.taglibs.ReusableTagLibs</tagclass>

<info>Localization function</info>

<attribute>

<name>property</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

<dynamic-attributes>true</dynamic-attributes>

</attribute>

<attribute>

<name>context</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

<dynamic-attributes>true</dynamic-attributes>

</attribute>

<attribute>

<name>encoding</name>

<required>false</required>

</attribute>

<dynamic-attributes>true</dynamic-attributes>

</tag>

</taglib>

Step 3: Build the bundle.

Step 4: Include the tag library in your jsp

<%@taglib prefix=”reusable” uri=”/apps/reusablecomponents/dm/src/impl/src/main/resources/META-INF/reusable.tld” %>

Use the tag in your JSP like this:    <reusable:label  property=”hello” context=”<%= pageContext %>”/>

Leave a comment