[ Team LiB ] Previous Section Next Section

Creating a Tag with a Body

For your first step in creating a tag with a body, you must write the tag handler class. Using the helper class, you should extend the abstract class BodyTagSupport as shown in Listing 18.4 (DisplayItem.java).

Listing 18.4 DisplayItem.java
package com.gams.ejbs.item;

import java.io.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.sql.Date;

public class DisplayItem extends BodyTagSupport

Next, you must implement setters/getters, and the methods doStartTag(), doAfterBody(), and doEndTag() as shown here:

    private ItemValue item = null;
    private BodyContent bodyContent = null;

  public DisplayItem()

  public void setItem(ItemValue iv)
    item = iv;

  public void setBodyContent(BodyContent bc)
    bodyContent = bc;


  public int doStartTag() throws JspException

        Float currentBid = item.getCurrentBid();
        String strCurrentBid = "";
        if (item.getCurrentBid() == null)
         {item.setCurrentBid(new Float(0.0));}
        { strCurrentBid = currentBid.toString(); }

        return EVAL_BODY_TAG;

  public int doAfterBody()
     String strDescription = null;
     if ( null != item.getDescription())
        {strDescription = "Description: " + item.getDescription() + bodyContent.getString();}
     else {strDescription = bodyContent.getString();}

     return SKIP_BODY;

  public int doEndTag() throws JspException
         pageContext.getOut().println("<tr><td>" + item.getItemId() + "</td><td>" + item
graphics/ccc.gif.getTitle() + "</td><td>" + item.getCurrentBid() + "</td><td>" + item.getEndDateTime() +"<
         pageContext.getOut().println("<tr><td colspan=\"4\"> <b>Additional Information: <
graphics/ccc.gif/b>" + item.getDescription() + "</td></tr>");

        return EVAL_PAGE;
      catch (IOException ex)
        throw new JspException("Error: IOException while writing to client");
      catch (Exception x)
        throw new JspException("Error: Exception while writing to client");

Next you must modify the existing (or create a) tag library descriptor file by adding the tag definition code shown here:

  <description>Displays Item Information/with html table tags</description>

For our example, the tag library descriptor file name is META-INF/taglib.tld. To complete packaging, you should jar the tag library descriptor, tag handler class, and supporting files, if any, as previously discussed when creating a simple tag.

Copy the resulting JAR file to the WEB-INF/lib directory of your target Web application. At this point, you must register the JAR file within the Web application's web.xml file as discussed previously. You can now use the body tag within your JSP pages as discussed at Chapter 17. An example of using the body tag is shown in Listing 18.5.

Listing 18.5 displayItem.jsp
<%@ page import="com.gams.ejbs.item.*, java.sql.Date" %>
<%@ taglib uri="itemDisplay.tld" prefix="item" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

     <title>Global Auctions - Display Item</title>

<table width="500">
     <tr><td><b>Item Id</b></td><td><b>Title</b></td><td><b>Current Bid </b><
graphics/ccc.gif/td><td><b>Auction Ends</b></td></tr>
    ItemValue iv = new ItemValue(new Integer(101), "Ming Chair", new Float(550.50), new
graphics/ccc.gif Date(102, 06, 30));

    //ItemValue iv = (ItemValue)request.getAttribute("item");
    <item:displayItem item="<%= iv %>">
    <!-- Optional Information -->
       This Ming Chair has been certified as authentic. Documentation available upon request.


Viewed through a Web browser, file displayItem.jsp yields the display at Figure 18.4.

Figure 18.4. Browser view of displayItem.jsp.


    [ Team LiB ] Previous Section Next Section