Why creating a custom tag library?
One of the best practice on how to write jsp, is do not mix presentation logic with business logic. This should mean to minimise or do not use scriptlets at all. Instead you should use a combination of EL expressions and Custom Tag Libraries (including the JSTL).
Sometimes scriptlets are just faster for reaching your goal and we are not purist to say never use scriplets, just try to find your right balance in coding without adding a lot of business logic inside your components jsp.
One of the best practice on how to write jsp, is do not mix presentation logic with business logic. This should mean to minimise or do not use scriptlets at all. Instead you should use a combination of EL expressions and Custom Tag Libraries (including the JSTL).
Sometimes scriptlets are just faster for reaching your goal and we are not purist to say never use scriplets, just try to find your right balance in coding without adding a lot of business logic inside your components jsp.
Step 1. Add your Tag Library Descriptor
Create your Tag Library Descriptor file inside of the META-INF directory within your OSGi Bundle (e.g. my-custom-tag-library.tld).
IMPORTANT: In your Tag Library Descriptor (.tld file), for the value of the tag, you must specify a full and unique URL to identify your Tag Library within the CQ environment. If you only specify a relative URI, CQ often fails to find your library.
Create your Tag Library Descriptor file inside of the META-INF directory within your OSGi Bundle (e.g. my-custom-tag-library.tld).
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">
<tlib-version>1.0.0-SNAPSHOT</tlib-version>
<short-name>shortname</short-name>
<uri>http://codebay-innovation.com/my-custom-taglib</uri>
<tag>
<name>myCustomTag</name>
<tag-class>com.codebay.examples.taglib.MyCustomTag</tag-class>
<info>Just a sample taglib</info>
</tag>
</taglib>
IMPORTANT: In your Tag Library Descriptor (.tld file), for the value of the tag, you must specify a full and unique URL to identify your Tag Library within the CQ environment. If you only specify a relative URI, CQ often fails to find your library.
Step 2. Create your Tag Library class
Just create your tag library class as normal.
Step 3. Use your custom tag library from jsp
Once you have deployed your custom tag library into your AEM instance, you can just use it into your components jsp with the standard mechanism.
<%@ taglib uri="https://aemcorner.com/my-custom-taglib" prefix="myUtils" %>
<myUtils:myCustomTag/>
Just create your tag library class as normal.
package com.codebay.examples.taglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* Just a Taglib example.
* @author CodeBay Innovation
*/
public class MyCustomTag extends TagSupport {
@Override
public int doEndTag() throws JspException {
try {
pageContext.getOut().print("Hello from JSP!");
} catch (IOException e) {
throw new JspException(e.toString());
}
return EVAL_PAGE;
}
}
Step 3. Use your custom tag library from jsp
Once you have deployed your custom tag library into your AEM instance, you can just use it into your components jsp with the standard mechanism.
<%@ taglib uri="https://aemcorner.com/my-custom-taglib" prefix="myUtils" %>
<myUtils:myCustomTag/>
No comments:
Post a Comment
If you have any doubts or questions, please let us know.