[ Team LiB ] Previous Section Next Section

Creating a Simple Tag

For your first step in creating a simple tag, you must write the tag handler class. Using the helper class, you should extend the abstract class TagSupport as shown in Listing 18.2, SummaryDisplayItem.java.

Listing 18.2 SummaryDisplayItem.java
package com.gams.ejbs.item;

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

public class SummaryDisplayItem extends TagSupport
{

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

  ItemValue[] itemArray = null;

  public SummaryDisplayItem()
  {
  }

  public void setItemArray(ItemValue[] iAr)
  {
        itemArray = iAr;
        System.out.println("SummaryDisplayItme:1-ParamConstructor: Item array set.....");
  }
  public int doStartTag() throws JspException
  {
       try
       {
          for(int i=0; i<itemArray.length; i++)
          {
             ItemValue iv = itemArray[i];
             Float currentBid = iv.getCurrentBid();
             String strCurrentBid = "";
             if (iv.getCurrentBid() == null)
               {strCurrentBid = "None";}
             else
             {
                strCurrentBid = currentBid.toString();
             }
             pageContext.getOut().println("<tr><td>" + iv.getItemId() +"</td><td>" + iv
graphics/ccc.gif.getTitle() + "</td><td>" + strCurrentBid + "</td><td>" + iv.getEndDateTime() +"</td></tr>");
          }
       return SKIP_BODY;
       }
       catch (IOException ex)
       {
           throw new JspException("Error: IOException while writing ♦ to client");
       }
    }
}

After compiling the tag handler class (use the Java compiler -d option to create your package directory), you must next create or modify an existing tag library descriptor file as shown here:

 <?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/dtd/web-jsptaglibrary_1_2.dtd">

 <!-- a tab library descriptor -->

 <taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.1</jsp-version>
  <short-name>wl</short-name>
  <uri>http://www.beasys.com/j2ee/tlds/wl-jsptaglibrary_1_0.tld</uri>

  <tag>
   <name>summaryDisplayItem</name>
   <tag-class>com.gams.ejbs.item.SummaryDisplayItem</tag-class>
   <bodycontent>empty</bodycontent>
   <description>Displays Item value Array/with html table tags</description>
   <attribute>
    <name>itemArray</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
   </attribute>
  </tag>
 </taglib>

Name this file taglib.tld and save it to the META-INF folder. To complete packaging, you should jar the tag library descriptor, tag handler class, and supporting files, if any, into one JAR file. To jar, the tag library descriptor should be placed in directory META-INF/lib with the classes deployed within their designated package structure. Your class package structure and the META-INF directory should share a common root directory. The following command jars our example:


$ jar cv0f itemDisplay-tags.jar com META-INF

NOTE

You should notice the 0 jar command parameter. Some HTML documents do not execute properly if compressed.


Copy the resulting JAR file to the WEB-INF/lib directory of your target application. At this point, you must register the JAR file within the application's web.xml file as shown here:


<taglib>
   <taglib-uri>
    itemDisplay.tld
   </taglib-uri>
   <taglib-location>
    /WEB-INF/lib/itemDisplay-tags.jar
   </taglib-location>
 </taglib>

You can use the simple tag within your JSP pages as discussed in Chapter 17. An example of using the simple tag is shown in Listing 18.3 (FindItems.jsp).

Listing 18.3 FindItems.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 - Find Items</title>
</head>
<h1>Global Auctions - Find Items</h1>
<body>
<table width="500">
    <tr><td><b>Item Id</td><td>Title</td><td>Current Bid</td><td> 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 iv2 = new ItemValue(new Integer(202), "Ming Horse", new Float(1550.50), new
graphics/ccc.gif Date(102, 06, 30));
   ItemValue[] ivArray = new ItemValue[]{iv, iv2};

   //ItemValue[] ivArray = (ItemValue[])request.getAttribute("itemdata"); (used if data
graphics/ccc.gif retrieved from request object)
%>
   <item:summaryDisplayItem itemArray="<%= ivArray %>"/>
</table>
</body>
</html>

Viewed through a Web browser, the findItems.jsp file yields the displayed in Figure 18.3.

Figure 18.3. Browser view of FindItems.jsp.

graphics/18fig03.gif

    [ Team LiB ] Previous Section Next Section