[ 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
          for(int i=0; i<itemArray.length; i++)
             ItemValue iv = itemArray[i];
             Float currentBid = iv.getCurrentBid();
             String strCurrentBid = "";
             if (iv.getCurrentBid() == null)
               {strCurrentBid = "None";}
                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"

 <!-- a tab library descriptor -->


   <description>Displays Item value Array/with html table tags</description>

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


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:


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

<title>Global Auctions - Find Items</title>
<h1>Global Auctions - Find Items</h1>
<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 %>"/>

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

Figure 18.3. Browser view of FindItems.jsp.


    [ Team LiB ] Previous Section Next Section