Section 15.2. Writing a Simple Custom Tag

   

15.2 Writing a Simple Custom Tag

In this section we walk through the entire process of writing and deploying a custom tag. We'll start with the tag definition itself, Message.java , which just prints a message to the browser.

15.2.1 Message.java

 /*   * File: Message.java  * Purpose: demo simple custom tag by displaying message  * passed in custom tag attribute.  */ package javaforcf.ch15; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class MessageTest extends TagSupport {     private String message="";     public void setMessage(String m) {         this.message = m;     }     public String getMessage() {         return message;     }     public int doStartTag() throws JspException {         try {             pageContext.getOut().print("Hello" +                 this.getMessage());         }         catch (Exception ioException ) {             System.err.print(ioException.toString());             throw new JspException(ioException );         }         return SKIP_BODY;     } } 

This is a very simple custom tag. In order to call the tag, we have to write a TLD for it, which looks like this:

15.2.2 cart.tld

 <?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"> <taglib>     <tlib-version>1.0</tlib-version>     <jsp-version>1.2</jsp-version>     <short-name>a simple demo tag</short-name>     <description>displays a pleasant greeting</description>     <tag>         <name>message</name>         <tag-class>javaforcf.chp15.MessageTest         </tag-class>         <body-content>empty</body-content>         <description>test: show a message</description>         <attribute>             <name>message</name>             <required>true</required>             <rtexprvalue>false</rtexprvalue>             <type>java.lang.String</type>         </attribute>     </tag> </taglib> 

This TLD first declares that we require version 1.2 of JSP (though there is nothing really in the tag that requires it). We give it a short name and a description, both of which are arbitrary text strings for human readers. Then we write the <tag> element, which will hold all of the information that the container is interested in regarding this tag.

First we give it a name, and then we tell the container where it can find the tag handler class file. We specify the body content as empty, which means that we will extend TagSupport , not BodyTagSupport . Because our tag has an attribute, we need to specify what the attribute is called and specify whether it is required. Then we specify whether this tag has a return expression value and what type the attribute accepts, and we're done.

Once you have written and compiled the tag handler and written the TLD, you then need to write a JSP that imports the TLD and calls the tag. That JSP might look like this:

15.2.3 MessageCaller.jsp

 <%-- File: simpletag.jsp       Purpose: call a custom tag  --%> <%@page contentType="text/html"%> <%@ taglib prefix="cart" uri="/WEB-INF/cart.tld" %> <html> <body> <%-- call the tag--%> <h1><cart:message message="I am a custom tag."/></h1> </body> </html> 

The tag prefixes the output with "Hello" and prints it to the browser. The important part of the code is shown here:

 try {      pageContext.getOut().print("Hello" +        this.getMessage());           } 

The output is shown in Figure 15.1.

Figure 15.1. Custom tag display.

graphics/15fig01.gif

This also demonstrates use of JSP's PageContext class, which we'll put to work in the next chapter. Its purpose is to offer one point of reference for many of the attributes of a page. The pageContext variable stores the value of the PageContext object for the current page. Note that CFMX makes use of the PageContext variable for its forward() and include() methods .

To deploy the tag, mention the library in your web.xml file, put the TLD in the directory specified in web.xml , put the class file in the directory specified in the TLD, write a JSP that calls it, restart Tomcat, and you're in business.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

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