Recently do a tree-shaped structure of the display, request the target page, the background only return a simple list, although there is a desire to do some work in the JSP page simplification, but too cumbersome, other tags can not meet the needs, so can only do one by themselves. Using TLD tags simplifies JSP code and can be reused later, so for these two benefits, using a custom TLD tag is a good choice. This is just a simple example of all the strings being capitalized.
1. Defining the class of the TLD
The method defined should be the static method.
public class TestFunction {public static string Stringuppercase (string target) { return target.touppercase (); c2/>}}
2. Adding TLD Tags
<?xml version= "1.0" encoding= "UTF-8"?> <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 "> <tlib-version>1.0</ tlib-version> <short-name>fu</short-name> <uri>/web-inf/tags/function.tld</uri > <function> <name>stringUpperCase</name> <function-class> Test.tld.testfunction</function-class> <function-signature>java.lang.string StringUpperCase ( java.lang.String) </function-signature> </function> </taglib>
<short-name> represents the invocation name of the claim label.
<uri> represents the location of the TLD label and the TLD label should be defined in the Web-inf. Here I put in Web-inf's tags folder.
<function-class>tld the class of the method that the label runs on.
<function-signature> declares the type, method name, and method parameters of the method returned. The method parameters can be list,int and so on.
3. The TLD label is declared in Web. xml
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "version=" 3.0 "> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </ welcome-file-list> <jsp-config> <taglib> <taglib-uri>/tags/function</ taglib-uri> <taglib-location>/WEB-INF/tags/function.tld</taglib-location> </taglib > </jsp-config> </web-app>
4. Using custom TLD Tags
<%@ taglib prefix= "FN" uri= "/tags/function"%>${fn:stringuppercase (target)}
The TLD label has been declared before it is used. Target is a string that is stored in the background in request or session.
5. Summary
Good use of TLD tags can make your page more elegant at a critical time, in the use of a certain JSP code, can be encapsulated to make the page more concise, the next time to use more convenient.
Simplifying the tedious operation of JSPs with custom TLD tags