Friday, March 16, 2012

Writing Custom Tag in Jstl

Any programmer will we more keen to optimize his/her code in performance vise and as well as code redundancy. For achieving the same various standards and design pattern are been followed. I would like to show how to write custom tags and use it in web application ,we may have to follow some simple steps to achieve the same.

This post has very basic example. will try to post the work around for ajax call using in custom tag in my next post ;) .

Lets kick start !

1.Write the tag handler class.
2.Create the tag library descriptor (TLD).
3.Make the TLD file and handler classes accessible.
4.Reference the tag library.
5.Use the tag in a JSP page.

Step 1.Write tag handler class

The first thing we need to do is write the tag handler class. A tag handler is an object invoked by the JSP runtime to evaluate a custom tag during the execution of a JSP page that references the tag. The methods of the tag handler are called by the implementation class at various points during the evaluation of the tag. All tags must implement theTag interface. We do not need to include the body content in our example TodayTag, because it only displays the current date in the user defined format. As such, our handler class will implement the Tag interface (typically by extending the TagSupport class). If we were to create a tag capable of processing body content, we would need to implement the BodyTag interface (typically by extending the BodyTagSupport class).


package jstl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

/**
* @author yogesh
*/
public class TodayTag extends TagSupport{
private String mFormat;
public void setFormat(String pFormat) {
mFormat = pFormat;
}
public int doAfterBody() throws JspException {
try {
JspWriter out = pageContext.getOut();
Date today = new Date();
SimpleDateFormat dateFormatter = new SimpleDateFormat(mFormat);
out.print(dateFormatter.format(today));
} catch(IOException ioe) {
throw new JspException("Error: " + ioe.getMessage());
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
return SKIP_PAGE;
}
}

Step 2: Create the Tag Library Discriptor(TLD)


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd">
1.0
ct
/WEB-INF/customTag
today
jstl.TodayTag
empty
This Tag Displayes current server date in usser defined format
format
true
Provide a display format
false

Step 3 : Make the TLD file and handler classes accessible

The third step is to make the class or classes and TLD accessible to the web application. There are two ways of doing this. We can either package the classes and the TLD together into a JAR file and then store the JAR file in the web application's lib directory, or we can place the class files loosely in the classes subdirectory and place the TLD file somewhere beneath the web application's WEB-INF directory.

Step 4. Reference the tag library

The fourth step is to make the class or classes and TLD accessible to the web application. In order to use the tag, we have to reference it, and this can be done in three ways:

Reference the tag library descriptor of an unpacked tag library. For example:
Collapse | Copy Code
<%@ taglib uri="/WEB-INF/customTag.tld" prefix="ct" %>
Reference a JAR file containing a tag library. For example:
Collapse | Copy Code
<%@ taglib uri="/WEB-INF/lib/customTag.jar" prefix="ct" %>
Define a reference to the tag library descriptor from the web-application descriptor (web.xml), and define a short name to reference the tag library from JSP.
Collapse | Copy Code
customTag
/WEB-INF/customTag.tld
Next, we add a JSP declaration to any page that will need to use the custom tag library:


<%@ taglib uri="customTag" prefix="ct" %>

Step 5. Use the tag in a JSP page

<%@ taglib uri="/WEB-INF/customTag.tld" prefix="ct" %>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
"http://www.w3.org/TR/html4/loose.dtd">

That`s it ! Try and have fun !!!!!!
JSTL Custom Tag Today

No comments:

Post a Comment