Custom Jstl tags and libraries

Source: Internet
Author: User
Tags tld

One, custom Jstl label

1. Write the label Processing class:

(1) Implement Simpletag interface, through the Setjspcontext () method can get to Jspcontext object, is actually PageContext object.

Complete the logic in the Dotag () method by JspWriter out = Jspcontext.getout (); Gets the out object that can be output to the page. Such as:

/*** MyTag2 * *@authorSolverpeng * @create 2016-07-03-10:20*/ Public classMyTag2ImplementsSimpletag {PrivateJspcontext Jspcontext =NULL; @Override Public voidDotag ()throwsjspexception, IOException {simpledateformat SimpleDateFormat=NewSimpleDateFormat ("Yyyy-mm-dd"); String Datastr= Simpledateformat.format (NewDate ()); JspWriter out=jspcontext.getout ();    Out.write (DATASTR); } @Override Public voidsetParent (Jsptag jsptag) {} @Override PublicJsptag getParent () {return NULL; } @Override Public voidSetjspcontext (Jspcontext jspcontext) { This. Jspcontext =Jspcontext; } @Override Public voidsetjspbody (Jspfragment jspfragment) {}}
MyTag2

(2) To create a new custom TLD file under Web-inf, you can refer to the implementation of some of the tags of JSTL itself, such as C.tld. Then register the label. Such as:

<?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">    <Display-name>Solverpeng Core</Display-name>    <tlib-version>1.1</tlib-version>    <Short-name>Solverpeng</Short-name>    <URI>Http://solverpeng.com/tags</URI>    <Tag>        <name>Showdate</name>        <Tag-class>Com.nucsoft.myservlet.tag.MyTag2</Tag-class>        <body-content>Empty</body-content>    </Tag>    </taglib>
Solverpeng Core Tag

(3) Import the custom tag library in the page and use it. Such as:

<%@ Page ContentType="Text/html;charset=utf-8"language="Java" %><%@ taglib Prefix="s"URI="Http://solverpeng.com/tags" %><HTML><Head>    <title>Title</title></Head><Body>    <s:showdate/></Body></HTML>
tag.jsp

2. Inherit the Simpletagsupport. In the Simpletagsupport class has completed the Jsptag, Jspcontext,jspfragment injection operations, can be used directly.

3. Places to be aware of

In the custom label TLD file, the contents of the Body-content node in the tag node have four optional values, which are described separately:

(1) Tagdependent: The contents of the label body will not be modified and processed intact to the label processing class

(2) Empty: No label body

(3) Scriptless: Indicates that the tag body supports plain text, El expressions, Jstl and other tags, does not support JSP script fragments and JSP expressions

(4) JSP: Not available under the Simpletag label system

You can get to the other 8 large JSP hidden objects through Jspcontext.

If a custom label with attributes is obtained by providing the Setxxx () method in the target processing class, in the TLD file, the child node attribute is added to the tag element.

The three child nodes under the attribute node are described:

(1) Name: property name, and the property name defined by the Setxxx () method in the target processing class is consistent

(2) Required: whether it is necessary to enter the

(3) Rtexprvalue: Whether EL expression is supported

If you need to process the label body, you need to do a series of processing in the target processing class:

(1) The Jspfragment object can be obtained through this.getjspbody () in the Dotag () method

(2) Execute the Invoke () method of the Jspfragment object, if the execution of the label body is not required to process, but the direct output, the Invoke () method parameter passed null is OK.

If you need to process the tag body after execution, you need to pass in a write object, after calling the Invoke () method, and then processing the Write object. Such as:

This isnew= = this. Getjspcontext (). Getout (); Writer.print (result);
View Code

Second, custom Jstl function library

1. Define the function processing class, which requires that the class and method must be public and that the method must be static. Such as:

/**@author*/Publicclass  MyFunction    {  Public Static String Hello () {        returnnew  Date (). toString ();}    }
MyFunction

2. Create a new custom TLD file under Web-inf, you can refer to the implementation of some of the tags in JSTL itself, such as C.tld. Then register the function. Such as:

<?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">    <Display-name>Solverpeng Core</Display-name>    <tlib-version>1.1</tlib-version>    <Short-name>Solverpeng</Short-name>    <URI>Http://solverpeng.com/tags</URI>    <function>        <name>Hello</name>        <Function-class>Com.nucsoft.myservlet.function.MyFunction</Function-class>        <function-signature>java.lang.String Hello ()</function-signature>    </function></taglib>
Solverpeng tag TLD

3. Import the Custom library of functions in the JSP page and use it on the page, such as:

<%@ Page ContentType="Text/html;charset=utf-8"language="Java" %><%@ taglib Prefix="s"URI="Http://solverpeng.com/tags" %><HTML><Head>    <title>Title</title></Head><Body>${s:hello ()}</Body></HTML>
tag.jsp

Third, the jstl in a non-common label C:fortokens

<%Pagecontext.setattribute ("Str", "Aa,bb,ee,mm,tt"); %><C:fortokensItems= "${str}"delims=","var= "Item">${item}</C:fortokens>
C:fortokens

Custom Jstl tags and libraries

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.