JSP 2.x custom tag and jsp2.x custom tag

Source: Internet
Author: User

JSP 2.x custom tag and jsp2.x custom tag

JSP 1.x labels are flexible but complex. JSP 2.x provides a set of simplified tag syntax.


SimpleTagSupportIs a subclass of the SimpleTag interface. It also supports parameters and tag bodies. doTage is the core method.

 

Public AddTag extends SimpleTagSupport {private int num1; private int num2; public void doTag () throws JspException, IOException {this. getJspContext. getOut (). write ("the sum of two numbers equals to:" + (num1 + num2);} // The setter and getter methods are omitted}
 

The same function is used to calculate the sum of two numbers. The configuration is the same as that of JSP1.x.

 

The tag body of JSP 1.x is injected to BodyTag through setBodyContent and obtained through getBodyContent. SimpleTagJspFragmentObject instance implementation

 

// This label is used to convert the characters into public void doTag () throws JspException, IOException {StringWriter writer = newStringWriter (); JspFragment jspFragment = this. getJspBody (); // output to the specified writer through invoke. If the parameter is null, It is output to the default writer, // getJspContext (). getOut () jspFragment. invoke (writer); String content = writer. getBuffer (). toString (); this. getJspContext (). getOut (). print (content. toUpperCase ());}

 

The configuration is as follows:

<tag>      <name>tagname</name>      <tagclass>com.chen.tags.TagTest</tagclass>      <bodycontent>tagdependent</bodycontent >      <info>tag information</info></tag>


Here, <bodycontent> can only be tagdependent, not JSP or bodycontent

 

And JSP 1. x is different, JSP 2. the x tag can have multiple tag bodies, and the tag bodies can be called in different order and in different times. The <JSP: attribute>, in addition, the tld must be configured in combination with the fragment type.

<attribute>             <name>body1</name>             <required>false</ required>             <fragment>true</fragment ></attribute>


 

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.