Recipe 22.9 Creating a TLD for a Simple Tag Handler


Problem

You want to create a TLD for a simple tag handler.

Solution

Use the JSP 2.0-style TLD for the simple tag handler.

Discussion

The simple tag handler derives from the JSP 2.0 API, so you can use the TLD version from JSP 2.0 as well. Example 22-7 shows the taglib start tag and the various xmlns attributes that your TLD must reproduce exactly. Then, unless you are using JSP 2.0 TLD features such as the tag-file element, you can specify the tag element and its nested elements with the same XML syntax that you used for the prior TLD version.

Example 22-7. A JSP 2.0 TLD file for a simple tag handler
  <taglib xmlns="http://java.sun.com/xml/ns/j2ee"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xsi:schemaLocation=      "http://java.sun.com/xml/ns/j2ee       http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"     version="2.0" > <!-- THE REST OF THE XML CONTENT IS THE SAME AS THE JSP 1.2 TLD VERSION EXCEPT FOR <jsp- version>2.0</jsp-version> -->  <tlib-version>1.0</tlib-version>     <jsp-version>2.0</jsp-version>     <short-name>cbck</short-name>     <uri>com.jspservletcookbook.tags</uri>     <description>Cookbook custom tags</description>  <tag>         <name>simplelogo</name>         <tag-class>com.jspservletcookbook.SimpleLogoTag</tag-class>         <body-content>JSP</body-content>         <description>This tag writes a logo inside the JSP.</description>         <attribute>             <name>heading</name>             <required>true</required>             <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> 

To use the simple tag handler in a web application, place the TLD in a subdirectory of WEB-INF like WEB-INF/tlds . Or, store the TLD in a JAR file's META-INF directory or a subdirectory thereof. Then put the JAR in the WEB-INF/lib directory.


See Also

The JSP 2.0 specification web page: http://jcp.org/en/jsr/detail?id=152; Recipe 22.2-Recipe 22.3 on creating TLD files for tag libraries; Recipe 22.4-Recipe 22.5 on packaging a tag library 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 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