The following steps are required to develop a tag library in JSP2:
1. Development of custom label processing classes;
2. Create a *.tld file, each *.tld file corresponding to a tag library, each tag library can contain multiple tags;
3. Use a custom label in the JSP file.
First step: Develop a custom label class. The tag class needs to inherit javax.servlet.jsp.SimpleTagSupport; If the tag class contains attributes, each attribute must have getter and setter methods;
Override the Dotag () method, which is responsible for generating page number content.
such as the following code:
1 PackageLee;2 Importjava.io.IOException;3 Importjavax.servlet.jsp.JspException;4 ImportJavax.servlet.jsp.tagext.SimpleTagSupport;5 Public classHellotagextendsSimpletagsupport {6 //overriding the Dotag method7 @Override8 Public voidDotag ()throwsjspexception, IOException {9 //get the page output stream and output the stringTenGetjspcontext (). Getout (). Write ("Hello Tag" +Newjava.util.Date ()); One } A}
The second step: the CV TLD file can be copied directly under Tomcat's webapps\examples\web-inf\jsp2\ jsp2-example-taglib.tld file into its web-inf directory.
Then rename , modify the content, the code is as follows:
1<?xml version= "1.0" encoding= "GBK"?>2<taglib xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"4version= "2.0" >5<!--Description--6<description>a Tag Library exercising Simpletag handlers.</description>7<!--version--8<tlib-version>1.0</tlib-version>9<!--short name--Ten< Short-name>mytag</ Short-name> One<!--specify a tag library URI-- A<uri>http://tomcat.apache.org/mytag</uri> -<tag> -<description>outputs Hello, world</description> the<!--tag Library name-- -<name>helloWorld</name> -<!--label Handling class-- -<tag-class>lee. hellotag</tag-class> +<!--tag body Content-- -<body-content>empty</body-content> +</tag> A</taglib>
The third step: using a custom label, create a new JSP file with the following code:
1<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2pageencoding= "UTF-8"%>3<%@ taglib uri= "Http://tomcat.apache.org/mytag" prefix= "MyTag"%>4<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >567<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">8<title>insert title here</title>9Ten<body> One<mytag:helloworld/> A</body> -JSP2 a custom label begins