What is custom action element?

When standard actions and EL is not sufficient then you can use custom action element. You can also do it by scripting, but you should not use scripting in your page.
It looks like <my:doCustomThing>
There is a standard library of custom tags known as JSP standard tag library (JSTL). Some selected useful custom tags are there in JSTL.
In expression language function we use our own static method names. But custom tag handler do not use custom method names.

EXAMPLE:
We will make a custom tag which will show the current date.

The taglib descriptor(date.tld):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>2.0</jspversion>
<shortname>mytags</shortname>
<info>
Sample date tag implementation on Empty element
</info>
<tag>
<name>date</name>
<tagclass>utils.DateTag</tagclass>
<bodycontent>empty</bodycontent>
</tag>
</taglib>

The tag handler class(DateTag.java):
package utils;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class DateTag extends TagSupport
{
public int doStartTag() throws JspException
{
java.util.Date d=new java.util.Date();
try{
JspWriter out=pageContext.getOut();
out.println(d);
out.close();
}catch(Exception e){}
return SKIP_BODY;
}
}

Configuration in DD:
<web-app>
<taglib>
<taglib-uri>dateuri</taglib-uri>
<taglib-location>/WEB-INF/date.tld</taglib-location>
</taglib>
</web-app>

The jsp which will use the custom tag(date.jsp):
<%@taglib uri="dateuri" prefix="abc"%>
<abc:date/>

No comments:

Post a Comment