Tag library supporting dynamic attributes [04-12-30 | Liu Dong]

Source: Internet
Author: User
Tag library supporting dynamic attributes [04-12-30 | original site, Liu Dong]

In the JSP environment of previous versions, we must define the corresponding getter/setter method for each attribute in the tag library in the tag Library Class. This makes it impossible to add attributes based on the actual running environment. To solve this problem frequently encountered in practice, the tag library of jsp2.0 introduces a new feature-dynamic attribute, which uses the javax interface. servlet. JSP. tagext. dynamicattributes is implemented. This article uses a simple example to describe how to develop a tag library that supports dynamic attributes.

First, describe this simple example. We can add all the attribute values and output the result of adding them. Next we will start with the three elements of the tag library.

1. Tag library implementation class

To support dynamic attribute tags, you only need to implement the dynamicattributes interface on the basis of the original interface and complete the setdynamicattribute method,CodeAs follows:

Package Javayou. taglib;

Import java. Io. ioexception;

Import javax. servlet. jsp. jspexception;

Import javax. servlet. jsp. tagext. dynamicattributes;

Import javax. servlet. jsp. tagext. tagsupport;

/**

* @ Author winter Lau

* Demonstrate tag libraries that support dynamic attributes

*/

Public classNumberaddtagExtendsTagsupportImplementsDynamicattributes {

 PrivateInt Total = 0;

 

 PublicInt dostarttag ()ThrowsJspexception {

Try {

Pagecontext. getout (). Print (total );

} Catch (ioexception e ){}

ReturnEval_body_include;

}

/*

* This method is defined in the dynamicattributes interface.

* @ See javax. servlet. jsp. tagext. dynamicattributes # setdynamicattribute (

* Java. Lang. String, java. Lang. String, java. Lang. Object)

*/

 PublicVoid setdynamicattribute (string Uri, string localname, object value)

ThrowsJspexception

{

Total + = integer. parseint (value. tostring ());

}

 PublicVoid release (){

Total = 0;

}

}

Compile this class and copy the class file to the {webapp}/WEB-INF/classes/javayou/taglib directory.

2. Tag library definition file

Next, you must set the dynamic-attributes attribute for the tag that supports dynamic attributes in the tag description file javayou. TLD. The value is true. The configuration in this example is as follows:

<? XML version = "1.0" encoding = "gb2312"?>

<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">



<Description> javayou taglibs </description>

<Display-Name> javayou demo taglibs </display-Name>

<Tlib-version> 1.1 </tlib-version>

<Short-Name> demo </short-Name>

<URI> http://www.javayou.com/dev/jsp </uri>



<Tag>

<Name> Add </Name>

<Tag-class> javayou. taglib. numberaddtag </Tag-class>

<Body-content> Empty </body-content>

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

</Tag>



</Taglib>

Save the file in the/WEB-INF directory.

3. Use the tag library page

There are two ways to call the tag library. Let's take a look at the test page Code add. jsp.

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" %>

<% @ Taglib prefix = "Demo" uri = "http://www.javayou.com/dev/jsp" %>

<Demo: Add a1 = "1" A2 = "2" A3 = "3" A4 = "4"/>

& Nbsp;

<Demo: Add>

<JSP: attribute name = "A1"> 1 </jsp: attribute>

<JSP: attribute name = "A2"> 2 </jsp: attribute>

<JSP: attribute name = "A3"> 3 </jsp: attribute>

<JSP: attribute name = "A4"> 4 </jsp: attribute>

<JSP: attribute name = "A5"> 5 </jsp: attribute>

</Demo: Add>

In the first call Method on this page, we assign several attributes to the tag, but we didn't define these attributes in the tag definition. This is what dynamic attributes mean. A built-in tag JSP: attribute is added to JSP 2.0. You can select the specific method based on your needs, the second method is suitable for dynamic attributes that are generated by some other labels.

We can see that JSP 2.0 is still doing a lot of things in terms of ease of use. For example, this dynamic attribute can greatly simplify the development process. We will introduce some new features of JSP 2.0 one after another. Please stay tuned. (Www.javayou.com)

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.