Using the Custom Tag Body

   

Java™ 2 Primer Plus
By Steven Haines, Steve Potts

Table of Contents
Chapter 24.  Custom Tag Libraries


In the preceding section, we added the custom tag using the XML syntax:

 <jpp:stringTag /> 

This syntax states that this tag has no body and, therefore, no need for an explicit end tag. Custom tags are allowed to have bodies, however, as shown in the following snippet:

 <jpp:stringTag>  This is the body of the tag.  It will only show up if the implementing  class instructs it to.  </jpp:stringTag> 

The primary use of this is to provide a small amount of conditional processing in the tag. Small changes are needed to the example to accommodate the inclusion of the body. Listing 24.4 shows the taglib for a tag that contains a body.

Listing 24.4 The jpptaglib2.tlb File
 <?xml version="1.0" encoding="UTF-8" ?>   <!DOCTYPE taglib          PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"          "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">  <!-- a tag library descriptor -->  <taglib>  <!-- after this the default space is          "http://java.sun.com/j2ee/dtds/jsptaglibrary_1_2.dtd"     -->  <tlibversion>1.0</tlibversion>  <jspversion>1.1</jspversion>  <shortname>jpptaglib2</shortname>  <info>     A tag library from Java Primer Plus  </info>   <tag>  <name>stringTag2</name>  <tagclass>com.samspublishing.jpp.ch25.StringTag2</tagclass>  <bodycontent>JSP</bodycontent>  <info>a Hello World custom tag example</info>  </tag>  </taglib> 

The primary difference in this file is the changing of the bodycontent from EMPTY to JSP.

 <bodycontent>JSP</bodycontent> 

This tells the JSP engine to process the body of the JSP.

Listing 24.5 shows the implementation class file for this example.

Listing 24.5 The StringTag2.class File
 /*   * StringTag2.java   *   * Created on July 13, 2002, 12:38 PM   */  package com.samspublishing.jpp.ch25;  import javax.servlet.jsp.*;  import javax.servlet.jsp.tagext.*;  import java.io.*;  /**   *   * @author  Stephen Potts   * @version   */  public class StringTag2 extends TagSupport  {     public int doStartTag()     {        try        {           JspWriter out = pageContext.getOut();           out.print("Hello, String tag2 example ");        }catch (Exception e)        {           System.out.println("Error in StringTag2 class" + e);        }        return(EVAL_BODY_INCLUDE);     }     public int doEndTag()      {        try        {           JspWriter out = pageContext.getOut();           out.print("<br>");           out.print("<br>");           out.print("Hello from doEndTag() ");        }catch (Exception e)        {           System.out.println("Error in doEndTag()" + e);         }        return(EVAL_PAGE);     }  } 

There are several changes in this class. The return value has changed from SKIP_BODY to EVAL_BODY_INCLUDE, which, as you might guess, tells the JSP engine to process the body also.

In addition to including the body, changing from SKIP_BODY to EVAL_BODY_INCLUDE causes the doEndTag() method to be processed as well.

 public int doEndTag()  {     try     {        JspWriter out = pageContext.getOut();        out.print("<br>");        out.print("<br>");        out.print("Hello from doEndTag() ");     }catch (Exception e)     {        System.out.println("Error in doEndTag()" + e);     }     return(EVAL_PAGE);  }  

This method is similar to the doStartTag() method except that it runs at the end of the tag processing phase. It contains mostly print() method calls.

The JSP file has to be altered to add a body to the tag as well. Listing 24.6 contains this JSP file.

Listing 24.6 The StringTag2.jsp File
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  <html>  <head>  <%@ taglib uri="jpptaglib2.tld" prefix="jpp" %>   <title>A Simple Custom Tag2</title>  </head>  <body>  <h1>A Simple Custom Tag2</h1>  <br>  <b>  <jpp:stringTag2>  <br>  This is the body of the tag.  It will only show up if the implementing  class instructs it to.  <br>  </jpp:stringTag2>  <b>  </body>  </html> 

The tag now has a separate start and end tag.

 <jpp:stringTag2>  <br>  This is the body of the tag.  It will only show up if the implementing  class instructs it to.  <br>  </jpp:stringTag2> 

Figure 24.3 shows the result of running this example:

Figure 24.3. The tag body is only displayed if the implementing class instructs it to.

graphics/24fig03.gif

As you can see, the body of the tag, as well as the information in the doEndTag() method, are displayed following the information from the doStartTag() method.


       
    Top
     



    Java 2 Primer Plus
    Java 2 Primer Plus
    ISBN: 0672324156
    EAN: 2147483647
    Year: 2001
    Pages: 332

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net