Recipe 22.2 Creating a JSP 1.2 TLD for a Classic Tag Handler


Problem

You want to create a JSP 1.2 TLD file for one or more custom tags.

Solution

Create the XML file using the proper DOCTYPE declaration for a JSP 1.2 TLD.

Discussion

A TLD is an XML file that describes your custom tags, the tag's attributes (if any), as well as the Java classes that provide the tag's functionality. The JSP container uses this configuration file when it interprets custom tags that appear in JSP pages. If you are using a JSP v1.2 container, your tag library's TLD has the DOCTYPE declaration shown in Example 22-2. This TLD describes the tag handler of the previous recipe.

Example 22-2. The TLD file for a classic JSP 1.2 tag handler
 <?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>cbck</short-name>     <!-- Here is the URI you use with the 'taglib' directive in the JSP -->  <uri>com.jspservletcookbook.tags</uri>  <description>Cookbook custom tags</description>  <tag>         <name>logo</name>                  <!-- make sure to use the fully qualifed class name -->         <tag-class>com.jspservletcookbook.LogoTag</tag-class>         <body-content>JSP</body-content>         <description>This tag writes a logo inside the JSP.</description>         <attribute>             <name>heading</name>             <!-- The logo tag requires this attribute -->             <required>true</required>             <!-- The attribute can take a JSP expression as a value -->             <rtexprvalue>true</rtexprvalue>             <description>The heading level for the logo; 1 through 6.             </description>         </attribute>                  <attribute>             <name>image</name>             <required>true</required>             <rtexprvalue>true</rtexprvalue>             <description>The image name for the logo.</description>         </attribute>                             <attribute>             <name>width</name>             <required>true</required>             <rtexprvalue>true</rtexprvalue>             <description>The image width for the logo.</description>         </attribute>                              <attribute>             <name>height</name>             <required>true</required>             <rtexprvalue>true</rtexprvalue>             <description>The image height for the logo.</description>         </attribute>     </tag>  </taglib> 

In JSP 1.2 and 2.0, a JSP container automatically searches WEB-INF , as well as the META-INF directory of your application's JAR files, for any file that ends with the extension .tld .

Because .tld is a fixed extension, it is mandatory to give your tag library descriptor filenames that end in .tld .


The container then uses the information it finds in the TLD to interpret custom tags that the web application may use. For example, the container maps the uri elements it finds in the TLD to the URIs specified by any taglib directives in JSP files. Example 22-2 specifies a uri of com.jspservletcookbook.tags for the tag library that contains the logo tag. A taglib directive that uses this tag library in a JSP looks like this:

 <%@ taglib uri="com.jspservletcookbook.tags" prefix="cbck" %> 

When the logo tag appears later on in the JSP, the JSP container knows that the tag belongs in the tag library with the com.jspservletcookbook.tags uri value, and the container can evaluate the JSP's tag use based on the TLD's specification of the tag class and the tag's various attributes. Based on the TLD, the JSP container knows that the logo tag's attributes are all required, so a JSP that uses the logo tag and omits an attribute fails to compile.

See Also

The JSP 2.0 specification web page: http://jcp.org/en/jsr/detail?id=152; Recipe 22.3 on creating a JSP 2.0 TLD file for tag libraries; Recipe 22.4 and Recipe 22.5 on packaging tag libraries in a web application; Recipe 22.6 on using the custom tag in a JSP; Recipe 22.7 on handling exceptions in tags; Recipe 22.8 and Recipe 22.9 on creating a simple tag handler; Recipe 22.10 on using the simple tag handler in a JSP; Recipe 22.11-Recipe 22.14 on using a JSP tag file; Recipe 22.15 on adding a listener class to a tag library; the custom tag sections of Hans Bergsten's JavaServer Pages , Third Edition (O'Reilly).



Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326

Similar book on Amazon

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