Custom Label Development Steps
1) Write a generic Java class that inherits the Simpletagsupport class, called the tag processor class
PackageGz.itcast;Importjava.io.IOException;Importjavax.servlet.jsp.JspException;ImportJavax.servlet.jsp.tagext.SimpleTagSupport; Public classIftagextendsSimpletagsupport {Private Booleantest; Public voidSettest (Booleantest) { This. Test =test;} Public voidDotag ()throwsjspexception, IOException {//TODO auto-generated Method Stub if(test) { This. Getjspbody (). Invoke (NULL); }}}
2) Create a itcast.tld file in the Web-inf directory of the Web project, called the tag library's declaration file. (Refer to the TLD file of the core tag library)
<?xml version= "1.0" encoding= "UTF-8"? ><taglib xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version= "2.1" ><tlib-version>1.1</tlib-version>< Short-name>itcast</ Short-name><uri>http://gz.itcast.cn</uri><tag><name>if</name><tag-class>gz.itcast.iftag</tag-class><body-content>scriptless</body-content><attribute><name>test</name>< Required>true</required><rtexprvalue>true</rtexprvalue></attribute></tag></taglib>
3) Import the custom tag library in the header of the JSP page
<% @taglib uri="http://gz.itcast.cn" prefix="Itcast"%>
4) Use a custom label in the JSP
Import= "java.util.*" pageencoding= "Utf-8"%><% @taglib uri= "http://gz.itcast.cn" prefix= "Itcast" %><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >if test= "${10>5}" > Condition established </itcast:if> </body>
Java JSP Custom Tags