Custom Label (JSTL)

Source: Internet
Author: User

To customize a label:

1, determine the requirements, such as: with <my:date/> output current time

2, write Java class: Need to Implement Interface Javax.servlet.jsp.tagext.JspTag

The specific interfaces are:

Javax.servlet.jsp.tagext.SimpleTag, simple label, jsp2.0**
Javax.servlet.jsp.tagext.Tag, classic label

3. Writing TDL file (XML)

Demo:

1  Public classMyTagImplementsSimpletag {2     3     PrivatePageContext PageContext;4      Public voidsetjspcontext (Jspcontext pc) {5         6          This. pagecontext=(PageContext) pc;//settings PageContext7     }8     9      Public voidDotag ()throwsjspexception, IOException {Ten          OneSimpleDateFormat format=NewSimpleDateFormat ("Yyyy-mm-dd Hh:mm:ss:SSS"); AString Date=format.format (NewDate ()); - pagecontext.getout (). print (date);//output to page -     } the  -      PublicJsptag getParent () { -         //TODO auto-generated Method Stub -         return NULL; +     } -  +      Public voidsetjspbody (jspfragment arg0) { A         //TODO auto-generated Method Stub at          -}


1 <?XML version= "1.0" encoding= "UTF-8"?>2 3 <taglibxmlns= "Http://java.sun.com/xml/ns/javaee"4 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"5 xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"6 version= "2.1">

<!--determine the version of the current tag library--7 <tlib-version>1.0</tlib-version>8 <Short-name>My</Short-name> <!--recommended to introduce the current note library is, using the shorthand-- 9 <URI>Http://www.cnblogs.com/liuwt365/mytag</URI> <!--as the current tag library profile, starting with a name * content: Custom, globally unique--- Ten One <Tag> A <name>Date</name> <!--determine label name -- - <Tag-class>Cn.test.simpleTag.MyTag</Tag-class> <!--determining the implementation class -- - <body-content>Empty</body-content> <!--formatting the body contents of the current tag-- the </Tag> - - </taglib>
1 <%@ taglib URI="Http://www.cnblogs.com/liuwt365/mytag"prefix="my" %> <!--page Registration --2 3 <my:date/>

PS: If the label contains attributes, you need to set it in TDL: <my:maxvalue num1= "num2=" ></my:maxValue>

  

1 <Tag>2         <name>Max</name>3         <Tag-class>Cn.test.simpleTag.MyMaxTag</Tag-class>4         <body-content>Empty</body-content>5         <attribute>6             <name>Num1</name>7             <Required>True</Required>8             <Rtexprvalue>True</Rtexprvalue>9             <type>Java.lang.Integer</type>Ten         </attribute> One          A         <attribute>//Setting Properties -             <name>Num2</name>//Property name -             <Required>True</Requiredwhether >//must be True|false (Yes|no) (not required) the             <Rtexprvalue>True</Rtexprvalue>//runtime expression value run-time expressions -             <type>Java.lang.Integer</type>//Type (not required) -         </attribute> -     </Tag>

If the label body is included, the following:<my:toupper>juhjdksds</my:toupper> are set in TDL

<Tag>        <name>ToUpper</name>        <Tag-class>Cn.test.simpleTag.MyToUpperTag</Tag-class>        <body-content>Scriptless</body-content>

<!--determine the type of label body
* Empty: No label body
* JSP: Support all JSP syntax, use in tag
* Scriptless: Scripts for JSP pages do not support <%%>
* Tagdependent: The label body does not do the processing, the original output
-
</Tag>

If the tag body is included, the Java class

1  Public void setjspbody (jspfragment JF) {2         this. jspfragment=JF;   Get      3     }

When outputting to a JSP page

 1  stringwriter stringwriter=new  StringWriter (); //  get stream  2  this . Jspfragment.invoke (StringWriter); //  writes data to the stream  4  String data=stringwriter.tostring (). toUpperCase ();  5  this  .pagecontext.getout (). print (data);  7  
This.jspFragment.invoke(pagecontext.getout ()) =This.jspFragment.invoke ();

Custom Label (JSTL)

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.