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.

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">
Sample date tag implementation on Empty element

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();
JspWriter out=pageContext.getOut();
}catch(Exception e){}
return SKIP_BODY;

Configuration in DD:

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

No comments:

Post a Comment