[ 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));}
        else
        { 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();}
     item.setDescription(strDescription);

     return SKIP_BODY;
  }

  public int doEndTag() throws JspException
  {
      try
      {
         pageContext.getOut().println("<tr><td>" + item.getItemId() + "</td><td>" + item
graphics/ccc.gif.getTitle() + "</td><td>" + item.getCurrentBid() + "</td><td>" + item.getEndDateTime() +"<
graphics/ccc.gif/td><td></tr>");
         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:

<tag>
  <name>displayItem</name>
  <tag-class>com.gams.ejbs.item.DisplayItem</tag-class>
  <bodycontent>JSP</bodycontent>
  <description>Displays Item Information/with html table tags</description>
  <attribute>
   <name>item</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
</tag>

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">

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

<body>
<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.
    </item:displayItem>

</table>
</body>
</html>

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

Figure 18.4. Browser view of displayItem.jsp.

graphics/18fig04.gif

    [ Team LiB ] Previous Section Next Section