Java Reference
In-Depth Information
Example 18−9: DecorBox.java (continued)
JspWriter out = pageContext.getOut();
out.println("</td></tr></table></td></tr></table></div>");
}
catch (IOException e) { throw new JspException(e.getMessage()); }
// This return value says to continue processing the JSP page.
return EVAL_PAGE;
}
}
Deploying a Custom Tag
Before you can use the custom tag defined by DecorBox.java , you must create a
TLD for the custom tag library. Example 18-10 shows the TLD file for our tag
library (which consists of the single box tag). As you can see, the TLD is an XML
file. It provides information about the library, each of the tags it contains, and each
of the attributes supported by each tag.
The <uri> tag near the top of the TLD is an important one. It defines a universally
unique name by which the tag library is known. The name includes a version
number so that this preliminary version of the the library can be distinguished
from later versions. The purpose of the specified URI is to provide a name for the
tag library; the URI may also, but is not required to, function as an official down-
load location for the TLD file. This URI is the name that should identify the desired
tag library in a JSP <%@taglib> directive. As you'll see, the WEB-INF/web.xml file
allows you to define a mapping from tag library URIs to local TLD files. *
Example 18−10: WEB-INF/tlds/decor_0_1.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-jsptaglib_1_1.dtd">
<!-- The tags above say that this is an XML document, and formally -->
<!-- specify the document type. -->
<taglib> <!-- Define a tag library -->
<tlibversion>0.1</tlibversion> <!-- The version of this tag library -->
<jspversion>1.1</jspversion> <!-- The version of JSP -->
<shortname>decor</shortname> <!-- The common name for the library -->
<uri> <!-- A URL that uniquely identifies it -->
http://www.davidflanagan.com/tlds/decor_0_1.tld
</uri>
<info> <!-- A simple description of the library -->
A simple tag library for decorative HTML output
</info>
* There are other ways to specify the location of the TLD. If you abandon the idea of using a globally
unique URI to identify the tag library, you can use a local URI in the <%@taglib%> directive to point
directly to the local copy of the TLD. Or, instead of specifying the location of the TLD file itself, you
can specify the location of a JAR file that contains the tag library implementation. In this case, the JSP
container looks in the META-INF/ directory for the TLD file. See the JSP specification for full details.
Search WWH ::




Custom Search