Summary of functional tag library in JSP 2.0

Source: Internet
Author: User
Tags tld

I have always liked JSP 2.0, but I do not know enough about the tag library of JSP 2.0. Therefore, I learned that there is one type of tag library that can be called
The tag of the function-based tag library is mainly used to call static function methods through El (Expression Language), which can simplify the development of some labels.

Imagine that the voting results should be displayed in a voting system. Assume that one column shows the voting type. For example, the voting type is single-choice or multiple-choice (of course, generally no
The voting results are displayed in such a silly way), while keys are generally stored in the database, and values are generally displayed to users, in this case, you need to convert it to the user during page display.
First, we construct a class, such
Package Liao. vote;
Public class votefunction
{

Public static string changevotetype (INT value)
{
// Intvalue: for example, the key from the database
// Do the conversion here, which is omitted here ....

}
}

Then design a custom tag library, such as vote. TLD, with the following structure:
<? 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">

<Description> vote </description>
<Display-Name> vote </display-Name>
<Tlib-version> 1.1 </tlib-version>
<Short-Name> vote </short-Name>
<URI> http: // Liao </uri>

<Function>
<Name> changevotetype </Name>
<Function-class> Liao. Vote. votefunction </function-class>
<Function-signature> JAVA. Lang. String votetype (Java. Lang. String) </function-signature>
</Function>

... The form below is the same, there can be many <function>

</Taglib>
<Name> indicates the name of the function. changevotetype must be the same as the previously defined name. <function-class> indicates the full name of the class, including the package name.
<Function-signature> is a simple description of the function.

Of course, at last we need to define it in Web. xml.
<JSP-config>
<Taglib>
<Taglib-Uri> http: // Liao/vote </taglib-Uri>
<Taglib-location>/WEB-INF/taglib/vote. TLD </taglib-location>
</Taglib>
</JSP-config>
Place vote. TLD under the WEB-INF/taglib folder

For specific use, such as in the JSP page
<% @ Taglib prefix = "Vote" uri = "http: // Liao/vote" %>
Note that the URI here must be the same as the <taglib-Uri> set in Web. xml.

The following method is called in actual calls:
$ {Vote: changevotetype {vote. votetype }}
Vote. votetype is the value of the key called in the database, which is not detailed here.

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.