1.1 Prerequisites 1.2 Tag Creation Steps 1.3 Two Types of Tags 2 Tags That Do NOT Manipulate Body Contents 2.1 Determine Tag and Attributes 2.2 Create an Empty Web Application 2.3 Write Tag Handler 2.4 Write Tag Library Descriptor (TLD) 2.5 Register Tag Library 2.6 Use Tag Library in a JSP 3 Tags That DO Manipulate Body Contents 3.1 Determine Tag and Attributes 3.2 Create a Web Application 3.3 Write Tag Handler 3.4 Write Tag Library Descriptor (TLD) 3.5 Register Tag Library 3.6 Use Tag Library in a JSP 4 Using Tomcat 4.x and JSP 1.2 5 References 1 IntroductionThis document explains how to create, deploy, and use a Java custom tag with Tomcat 3.3.1 (JSP 1.1) running on a Windows 2000 system.1.1 Prerequisites
1.2 Tag Creation StepsCreating a Java Custom Tag consists of the following steps:
1.3 Two Types of TagsThere are two types of tags:
The following sections will show how to create each type. 2 Tags That Do NOT Manipulate Body Contents2.1 Determine Tag and AttributesFor the purpose of this article, assume that we need a tag that will display a string as an error message, displaying it in bold. The error will have a default color of red, but can be displayed in any other color. For example, to use the tag:<test:tagError>Invalid user name</test:tagError> <test:tagError color="blue">Invalid Password</test:tagError> Will produce: <font color="red"><b>Error: Invalid User Id</b></font><br> <font color="blue"><b>Error: Invalid Password</b></font><br> 22.2 Create an Empty Web ApplicationIn the directory %TOMCAT_HOME%\webapps\ create the following folders:
2.3 Write Tag Handlerpackage com.tek271.testTags; import java.io.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class TagError extends TagSupport { private String mColor = "red"; public void setColor(String aColor) { mColor = aColor; } // setColor public int doStartTag() throws JspException { try { JspWriter out = pageContext.getOut(); out.print("<font color=\""); out.print(mColor); out.print("\"><b>Error: "); } catch (IOException ex) { throw new JspTagException(ex.getMessage()); } return EVAL_BODY_INCLUDE; // other return: SKIP_BODY } // doStartTag() public int doEndTag() throws JspException { try { JspWriter out = pageContext.getOut(); out.println("</b></font><br>"); } catch (IOException ex) { throw new JspTagException(ex.getMessage()); } return EVAL_PAGE; // other return: SKIP_PAGE } // doEndTag() } // TagError Notice the following about the class:
2.4 Write Tag Library Descriptor (TLD)<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd"> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>testTld</shortname> <info>Testing jsp tags</info> <tag> <name>tagError</name> <tagclass>com.tek271.testTags.TagError</tagclass> <bodycontent>JSP</bodycontent> <!-- Also: empty, tagdependent --> <info>Show a string as an error message</info> <attribute> <name>color</name> <required>false</required> </attribute> </tag> </taglib>
2.5 Register Tag LibraryCreate a new file with the following contents:<?xml version="1.0" encoding="ISO-8859-1"?> <web-app> <taglib> <taglib-uri>/testTaglib</taglib-uri> <taglib-location>/WEB-INF/tlds/testTld.tld</taglib-location> </taglib> </web-app> Save the above file into %TOMCAT_HOME%\webapps\testTags\WEB-INF\web.xml This maps references for /testTaglib to our TLD. 2.6 Use Tag Library in a JSP<%@ taglib uri="/testTaglib" prefix="test" %> <html> <head> <title>Testing Tag Library</title> </head> <body> <test:tagError>Invalid user name</test:tagError> <test:tagError color="blue">Invalid Password</test:tagError> </body> </html>
If you view the source of this page, you should see the following: <html> <head> <title>Testing Tag Library</title> </head> <body> <font color="red"><b>Error: Invalid user name</b></font><br> <font color="blue"><b>Error: Invalid Password</b></font><br> </body> </html> 3 Tags That DO Manipulate Body ContentsIn the previous section, we created a tag that puts some HTML before and after the body contents of the tag; however, we did not change or manipulate the contents of the tag. This section will show you how to do that.The main difference from the previous section is that the tag handler will extend the BodyTagSupport class instead of the TagSupport class. In the following subsections, we will repeat the steps we did in section 2, but with a new tag that manipulates its body contents. 3.1 Determine Tag and AttributesWe will create a tag that displays a combo box; the items of the list will be specified in the body of the tag. For example:<test:tagList> one two three </test:tagList> Will display a combo box list of three items. Additionally, the tag has an optional parameter that can take a list of items and display them in the list if the tag does not have a body. For example: <% List items = new ArrayList(); items.add("AAA"); items.add("BBB"); items.add("CCC"); %> <test:tagList items="<%=items %>"/> Note how the attribute is of a type that is not a string or primitive. 3.2 Create a Web ApplicationWe will use the web application that we created in section 2.23.3 Write Tag Handlerpackage com.tek271.testTags; import java.io.*; import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class TagList extends BodyTagSupport { private List mItems; public void setItems(List aItems) { mItems = aItems; } public int doAfterBody() throws JspException { BodyContent body = getBodyContent(); String text = body.getString().trim(); List list = text.length() >0 ? text2List(text) : mItems; if (list == null) return SKIP_BODY; try { JspWriter out = body.getEnclosingWriter(); out.println("<select>"); String item; for (int i=0; i<list.size(); i++) { item = (String)list.get(i); if (item.length()==0) continue; out.println(" <option>" + item + "</option>"); } out.println("</select>"); } catch (IOException ex) { throw new JspTagException(ex.getMessage()); } // try return SKIP_BODY; } // doAfterBody() /** Break aText into a list of lines */ private List text2List(String aText) { List list = new ArrayList(); StringTokenizer tz = new StringTokenizer(aText, "\n\r"); String item; while (tz.hasMoreTokens()) { item = tz.nextToken().trim(); if (item.length() > 0) list.add(item); } // while return list; } // text2List() } // TagList Notice the following about the class:
3.4 Write Tag Library Descriptor (TLD)Open the tag library we created in the last section ìtestTld.tldî and add the following tag definition at the end just before the </taglib>:<tag> <name>tagList</name> <tagclass>com.tek271.testTags.TagList</tagclass> <bodycontent>JSP</bodycontent> <info>show a list of items</info> <attribute> <name>items</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
3.5 Register Tag LibraryIf you did the tag of section 2, then the TLD should be already registered, otherwise do section 2.5.3.6 Use Tag Library in a JSP<%@ page import="java.util.*" %> <%@ taglib uri="/testTaglib" prefix="test" %> <html> <head> <title>Testing Tag Library</title> </head> <body> <test:tagError>Invalid user name</test:tagError> <test:tagError color="blue">Invalid Password</test:tagError> <% List items = new ArrayList(); items.add("AAA"); items.add("BBB"); items.add("CCC"); %> <test:tagList items="<%=items %>"/> <test:tagList> one two three </test:tagList> </body> </html>
If you view the source of this page, you should see the following: <html> <head> <title>Testing Tag Library</title> </head> <body> <font color="red"><b>Error: Invalid user name</b></font><br> <font color="blue"><b>Error: Invalid Password</b></font><br> <select> <option>AAA</option> <option>BBB</option> <option>CCC</option> </select> <select> <option>one</option> <option>two</option> <option>three</option> </select> </body> </html> 4 Using Tomcat 4.x and JSP 1.2If you decide to use Tomcat 4.x and JSP 1.2, you should notice the following:
Note that the table here shows elements that were used in this example only. 5 ReferencesThe following link from Sun is a page for JSP Tag Libraries, it has a lot of useful links and references:http://java.sun.com/products/jsp/taglibraries.html |