Tag Libraries

One of the challenges in meeting the objectives of the JSP technology is to minimize the programming logic complexity to which content developers are exposed.

The JSP 1.1 specification introduced a new capability for creating custom JSP tag libraries, which allow for the reduction of complexity. The idea is for the developer to provide simple and easy to use custom tags that can be used by the content developers to invoke complex logic.

A Tag Handler Class

A custom tag is composed of a tag handler class. The tag handler class is responsible for telling the system what should be done when a specific tag is encountered. The class file contains the actual Java code that is executed during the request.

Tags can optionally have one or more attributes and a body, but neither is required. The simplest tag is one without a body or attributes; the most complex tag has a body as well as one or more attributes.

The following list shows examples of a tag without a body, a tag without a body but with attributes, and a tag with attributes and a body:

  • A tag without a body:

    <mytaglib:MyTag/>
    
  • A tag without a body but with an attribute:

    <mytaglib:MyTag count="11"/>
    
  • A tag with a body and an attribute:

    <mytaglib:MyTag count="10">
    This is the body. It can contain actions, directives and other things
    </mytaglib:MyTag>
    

For tags without a body, the tag handler class must implement the doStartTag method. Tags without a body are useful when you just want relatively fixed content (that is, something that is not very customizable from one reference in the tag to another) accessible to the content developer.

Attributes can be used with tags without a body to facilitate customization of the results. In such cases, the tag handler class must also implement a setter method corresponding to the attribute name and be prefixed with “set”. This permits the setting of the relevant attribute(s) prior to the call to the doStartTag method, thereby allowing different results based on the value of the attributes.

For tags with a body, the tag handler class must also implement the doEndTag method. The doEndTag generally does nothing more than instruct the system to continue on; however, it is possible for it to take other actions, such as abort the execution of the JSP.

The code example in Figure 11-1 shows a tag handler class for a tag without a body.

Figure 11-1. An example of a simple tag handler class
import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class MyTag extends TagSupport
{
public int doStartTag()
      {
      try {
        JspWriter out = pageContext.getOut();
        out.print("A simple tag example");
         } catch IOException e)
             {
             //handle exception
             }
      return (SKIP_BODY);
      }
      }

A Tag Library Descriptor

Tags are organized into tag libraries. The tag library descriptor (.tld) file contains the list of tag names and names of associated tag handler classes.

A tag library descriptor example is shown in Figure 11-2.

Figure 11-2. Tag library descriptor example
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP
Tag Library 1.2//EN" "http://java.sun.com/j2ee/dtd/web-
jsptaglibrary_1_2.dtd">

<taglib>
  <tlibversion>1.1</tlibversion>
  <jspversion>1.2</jspversion>
  <shortname>example</shortname>

 <tag>
   <name>BlankLine</name>
   <tagclass>com.taglib.homedirect.BlankLine</tagclass>
   <bodycontent>EMPTY</bodycontent>

    <info>Inserts a blank line
    </info>
 </tag>
</taglib>

Figure 11-3 shows how a custom tag is used from within a JSP.

Figure 11-3. Using a custom tag from within a JSP
<%@ taglib uri="MyUtils-taglib.tld">
<%<html>
...
<utils:BlankLine />
<!--inserts a blank line-->
...
</html>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.156.202