JSP Custom Labels

Source: Internet
Author: User
Tags tld

Two versions, JSP version 1.X and JSP 2.X version. There is also a simpler custom function.

First, JSP 1.X

1. Basic version: Implement Tag interface

The process is as follows

After the class is written, it is also configured in the TLD file. The TLD file can be placed under Web-inf, which will be automatically loaded into the.

If it is not under Web-inf, specify a location in Web. xml

<jsp-config>    <taglib>        <taglib-uri>xxx</taglib-uri>        <taglib-location> Xxx</taglib-location>    </taglib></jsp-config>

  

<?xml version= "1.0" encoding= "UTF-8"? ><taglib .....><taglibversion>1.0</taglibversion> <jspversion>1.1</jspversion><shortname>xxx</shortname><uri>xxxxxxx</uri> <info>xxxx</info><tag> <name>xxx</name> <tagclass>xxx</tagclass> < bodycontent>xxx</bodycontent> <info>xxx</info>      <attribute>          <name>xxx </name>          <required>true</required>          <rtexprvalue>true</rtexprvalue>      </attribute></tag></taglib>

  

Bodycontent There are three kinds of values, empty: not allowed to have the label body exists, JSP: Allow the existence of tags, can be JSP code, Tagdependent: Allow the existence of tags, but the tag body code will not be executed.

2. Advanced version: Inherit TagSupport class

There is already a default process, as long as you implement doStartTag () and Doendtag ().

If the label has parameters (attributes), simply inject the property through the setter method, and the attribute needs to be declared in the TLD file, as written above.

Rtexprvalue indicates whether an El expression or scriptlet<%=%> is allowed. If you have more than one parameter, set up multiple setter methods and configure them in the TLD file.

3: With label body: through Bodytag or Bodytagsupport.

The process is as follows:

Where Bodytagsupport already has a default process, it will follow the middle line all the way.

If you want the label body to be executed more than once, you can control the return value of Doafterbody (). The output in the Doafterbody method is written to the bodycontent cache, so the value fetched each time through Getbodycontent includes all previous outputs.

4: With Dynamic properties: Also need to implement the Dynamicattributes interface, and implement the Setdynamicattribute method of the interface, to save dynamic properties.

It needs to be set in the TLD file and added in the tag tag

<dynamic-attributes>true</dynamic-attributes>

5: Nested Custom labels

The lower label can get the object of the upper label through the GetParent method, and its properties are obtained through its Getter,setter method. The parent tag can get the contents of the tag body after the tag body execution is finished by getbodycontent. Or a child tag passes the relevant data through the parent tag's Getter,setter method to the parent tag.

Second, JSP 2.X

Implements the Simpletag interface, implements the Dotag method, and supports both parameters and tag bodies. The Simpletagsupport class is generally inherited when used.

The Out object is obtained within the Dotag through Getjspcontext (). Getout ().

If the tag body is obtained by getjspbody, the returned object is a jspfragment, and the call Jspfragment.invoke (writer) method writes the contents to the corresponding output, and writes to the page when writer is null.

At this time the TLD file to make the relevant changes, bodycontent into tagdependent

If you have more than one label body, you need to use the Jsp:attribute label.

<taglib:multiAttribute>    <jsp:attribute name= "xxx" >xxxx</jsp:attribute>    <jsp: attribute name= "XXX" >xxxx</jsp:attribute></taglib:multiAttribute>

This is to set two properties in the class, type Jspfragment. The properties are set in the TLD file. Remove Rtexprvalue, increase

<fragment>true</fragment>

Iii. Custom Functions

1. Write a function class that adds a static method to customize.

2. Describe the function in the TLD file.

<function>    <description>xxx</description>    <name>xxx</name>    < function-class>xxx</function-class>    <function-signature>xxx</function-signature>    <example>xxx</example></function>

Function-signature is similar to the return value of java.lang.String substring (java.lang.String, int) Full class name Method name (parameter full class name ...), if array, list and so on plus l

JSP Custom Labels

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.