1. First, customize the label class
A. The custom label class should inherit the parent class javax. servlet. jsp. tagext. SimpleTagSupport.
B. If the label class contains attributes, each attribute must have the getter and setter methods.
C. Rewrite the doTag Method
2. Create a TLD File
A. Copy the tomcat installation directory
B. Modify the tld file, as shown in figure
[Html]
<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">
<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> mytaglib. tld </short-name>
<! -- Define the tag library URL -->
<Uri> http://tomcat.apache.org/jsp2-example-taglib </uri>
<! -- Define a tag -->
<Tag>
<! -- Define the tag name -->
<Name> helloWorld </name>
<! -- Define tag processing class -->
<Tag-class> hs. HelloWorldTag </tag-class>
<! -- Empty TAG body defined -->
<Body-content> empty </body-content>
</Tag>
</Taglib>
<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">
<Description> A tag library exercising SimpleTag handlers. </description>
<Tlib-version> 1.0 </tlib-version>
<Short-name> mytaglib. tld </short-name>
<! -- Define the tag library URL -->
<Uri> http://tomcat.apache.org/jsp2-example-taglib </uri>
<! -- Define a tag -->
<Tag>
<! -- Define the tag name -->
<Name> helloWorld </name>
<! -- Define tag processing class -->
<Tag-class> hs. HelloWorldTag </tag-class>
<! -- Empty TAG body defined -->
<Body-content> empty </body-content>
</Tag>
</Taglib>
3. Use a custom tag Library (without a TAG body)
[Html]
<% @ Taglib uri = "http://tomcat.apache.org/jsp2-example-taglib" prefix = "mytag" %>
<% @ Taglib uri = "http://tomcat.apache.org/jsp2-example-taglib" prefix = "mytag" %> [html] view plaincopyprint?
<Mytag: helloWorld/>
<Mytag: helloWorld/>