Creating Taglibs


Another feature of the webdoclet task is the ability to create custom tags using appropriate XDoclet tags. Custom tags provide a high level of reuse for the JSP page developer. In this section we will create an example custom tag and show how to use webdoclet to handle most of the details. Consider the custom tag defined in the following listing.

 package customtag; import java.io.*; import javax.servlet.jsp.*;  import javax.servlet.jsp.taxext.*; /** * @jsp.tag name="NameTag" * @jsp.variable name-given="currentTry"  *               class="java.lang.Integer"  *               scope="NESTED"  *               declare="true" */ public class NameTag implements Tag {    private PageContext pc = null;    private Tag parent = null;    private String name = null;    private int try = 0;   public int doStartTag()                         throws JspException {      try {       if (name != null) {         pc.getOut().write("Your name is " + name);          pc.setAttribute("currentTry", new Integer(0));        } else {         pc.getOut().write("You aren't using the tag correctly.                 You need to set the name attribute");         try++;         pc.setAttribute("currentTry", new Integer(try));       }     } catch(Exception e) {         throw new JspException(e);     }        return SKIP BODY;   }   public int doEndTag()               throws JspException {     return EVAL PAGE;   }   /**    * @jsp.attribute required="true"    *                rtexprvalue=""    *                descripton="The name of the thing"   */   public String getName() {      return name;   }   public void setName(String s) {      name = s;   }   public void setPageContext(PageContext p)      pc = p;   }   public setParent(Tag t) {      parent = t;   }   public Tag getParent() {      return parent;    }   } 

All custom tags must have a corresponding Tag Library Descriptor (TLD) in order for the engine to know about the tag. The next listing shows the start of the TLD for the simple tag above.

 <taglib>   <tlibversion>1.0</tlibversion>    <jspversion>1.1</jspversion>    <shortname>Tags</shortname>    <tag>     <name>NameTag</name>      <tagclass>example.NameTag</tagClass>      <bodycontent>empty</bodycontent>      <info>First tag</info>     <attribute>       <name>name</name>        <required>true</required> ... 

Clearly, you can see why it is important that we have another option for creating the TLD. Although the file isn't complex, it can become quite large if you have many custom tags. It's easy to make mistakes. Our solution is to use Ant and webdoclet to generate the TLD for us. The next listing shows that task we will need to add to an Ant build script to handle the creation of our tag library. The subtask should be added to the GenerateDD target.

 <webdoclet desdir="./web/WEB-INF"> <fileset dir="./src">   <include name="* */*Servlet.java" />    <include name="* */*Tag.j ava" />  </fileset> <deploymentdescriptor                         servletspec="2.3"                        destdir="./web/WEB-INF" > <taglib uri="mytablib"         location="WEB-INF/tlds/mytablib.tld" /> <jsptaglib              jspversion= 1.2"               destdir="./web/WEB-INF/tlds"               shortname="Tags"               filename="mytablib.tld" /> 

Testing the Custom Tag

Now that we have a custom tag and an Ant build script that will handle the creation of the TLD as well as the custom tag class itself, it is time to write a JSP page to test the tag. The following listing shows an example JSP.

 %@page contacntType="text/html"%  %@taglib uri="mytaglib" prefix="mytag"%  <html> <head> <title>JSP Tag</title> </head> <body> <mytag:NameTag name="Jim">   Total tries = <%=currentTry%> <br/>  </mytag:NameTag> </body>  </html> 

In this example JSP, we use the NameTag tag and supply a specific name for the name attribute. We also access the currentTry variable defined by the custom tag.




Professional Java Tools for Extreme Programming
Professional Java Tools for Extreme Programming: Ant, XDoclet, JUnit, Cactus, and Maven (Programmer to Programmer)
ISBN: 0764556177
EAN: 2147483647
Year: 2003
Pages: 228

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