Custom JSTL labels and function libraries, and jstl label function libraries

Source: Internet
Author: User
Tags tld

Custom JSTL labels and function libraries, and jstl label function libraries

I. Custom JSTL labels

1. Compile the tag processing class:

(1) Implement the SimpleTag interface. The jspContext object can be obtained through the setJspContext () method, which is actually a pageContext object.

Complete the logic in the doTag () method. The out object obtained through JspWriter out = jspContext. getOut (); can be output to the page. For example:

/*** MyTag2 *** @ author solverpeng * @ create 2016-07-03-*/public class MyTag2 implements SimpleTag {private JspContext jspContext = null; @ Override public void doTag () throws JspException, IOException {SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd"); String dataStr = simpleDateFormat. format (new Date (); JspWriter out = jspContext. getOut (); out. write (dataStr) ;}@ Override public void setParent (JspTag jspTag) {}@ Override public JspTag getParent () {return null ;}@ Override public void setJspContext (JspContext jspContext) {this. jspContext = jspContext;} @ Override public void setJspBody (JspFragment jspFragment ){}}MyTag2

(2) In the WEB-INF to create a custom tld file, you can refer to the implementation of some of the JSTL labels, such as c. tld. Then register the tag. For example:

<? 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"> <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 on the page and use it. For example:

<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%> <% @ taglib prefix =" s "uri =" http://solverpeng.com/tags "%> 2. inherit from SimpleTagSupport. The injection of JspTag, JspContext, and JspFragment has been completed in the SimpleTagSupport class and can be directly used.

3. Notes

In the custom tag tld file, the content of the body-content node in the tag node has four optional values:

(1) tagdependent: submits a tag processing class without modifying or processing the content of the TAG body.

(2) empty: no label body

(3) scriptless: indicates that the label body supports labels such as plain text, EL expressions, and JSTL, but does not support JSP script fragments and JSP expressions.

(4) JSP: unavailable in SimpleTag tag system

 

You can use jspContext to obtain the other 8 JSP implicit objects.

 

If the tag is a custom tag with attributes, it is obtained by providing the setXxx () method in the target processing class. In the tld file, you must add the subnode attribute to the tag Element.

The three subnodes under the attribute node are described as follows:

(1) name: The property name, which is consistent with the property name defined by the setXxx () method in the target processing class.

(2) required: required

(3) rtexprvalue: whether EL expressions are supported

 

If you need to process the TAG body, you need to perform 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 you do not need to process the label body after execution but directly output the method, it is OK to pass in null as the parameter of the invoke () method.

If you need to process the executed label body, you need to input a Write object. After the invoke () method is called, The Write object is processed. For example:

JspFragment jspBody = this. getJspBody (); StringWriter out = new StringWriter (); jspBody. invoke (out); String content = out. toString (); String result = content. toUpperCase (); JspWriter writer = this. getJspContext (). getOut (); writer. print (result );View Code

 

Ii. Custom JSTL function library

1. Define the function processing class. The class and method must be public and the method must be static. For example:

/*** @ Author solverpeng * @ create 2016-07-03-*/public class MyFunction {public static String hello () {return new Date (). toString ();}}MyFunction

2. Create a custom tld file under the WEB-INF. You can refer to the implementation of some labels of JSTL, such as c. tld. Then register the function. For example:

<? 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"> <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 function library to the JSP page and use it on the page, for example:

<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%> <% @ taglib prefix =" s "uri =" http://solverpeng.com/tags "%>  

3. An Uncommon label in JSTL c: forTokens

<% PageContext. setAttribute ("str", "aa, bb, ee, mm, tt"); %> <c: forTokens items = "$ {str}" delims = ", "var =" item ">$ {item} </c: forTokens>C: forTokens

 

Related Article

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.