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 { publicstatic string Stringuppercase (string target) { return target.touppercase (); }}
2. Adding TLD Tags
<?XML version= "1.0" encoding= "UTF-8"?> <taglibxmlns= "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 Web-inf. Here I put in Web-inf's tags folder.
<function-class>TLD Tags Run the class of the method.
<function-signature> declares the method's return type, the method name, and the method's parameters. 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-appXmlns: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
<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
TLD tags seem useless, use it to make your page more elegant at the critical time, in the use of a certain JSP code, can be encapsulated, so that the page is more concise, the next time to use more convenient.
Simplifying the tedious operation of JSPs with custom TLD tags