Development and application of JSP custom label

Source: Internet
Author: User
Tags tld

First, the basic concept:
1. Tags (tag):

A label is an XML element that enables a JSP Web page to be concise and maintainable, and to easily implement the same JSP file to support multiple language versions. Because the label is an XML element, its name and attributes are case-sensitive

2. Tag libraries (Tag library):

A set of functionally similar, logically interrelated tags is called a tag library.

3. Tag library description file (Tag descriptor):

The tag library description file is an XML file that provides a mapping of the label references in the class and JSP in the tag library. It is a configuration file, and Web.xml is similar.

4. Label processing class (Tag Handle Class):

The label processing class is a Java class that inherits the TagSupport or extends the Simpletag interface, which enables you to implement the specific functions of a custom JSP tag

Second, the custom JSP label format:

1.

<% @ taglib prefix= "Someprefix" uri= "/sometaglib"%>

For the JSP container to be able to use custom behavior in the tag library, the following two conditions must be met:

1. Identify the label representing this custom behavior from a specified tag library

2. Find specific classes to implement these custom behaviors

The first requirement-find a custom behavior that belongs to that tag library-is done by the prefix of the label directive (Taglib directive ' s Prefix), so elements that use the same prefix on the same page belong to this tag library. Each tag library defines a default prefix that is used in a document in a tag library or in a page to insert a custom label. So, you can use prefixes other than those such as JSP,JSPX,JAVA,SERVLET,SUN,SUNW (which are all reserved words specified in the JSP white paper).

The URI attribute satisfies the second requirement above. Find the corresponding class for each custom behavior. This URI contains a string that the container uses to locate the TLD file. The name of all label processing classes in the tag library can be found in the TLD file

2.

<someprefix:sometag key= "somevalue"/>

When the Web application starts, the container searches all files that end with the. TLD from the Meta-inf of the directory structure of the Web-inf folder. That means they locate all the TLD files. For each TLD file, the container first obtains the URI of the tag library, and then creates a mapping relationship for each TLD file and the corresponding URI.

In the JSP page, we only need to match the specific tag library by using the tag library instruction with the URI attribute value

Third, the custom JSP label processing process:

1. To introduce a tag library in a JSP:

2. To use tag library labels in your JSP:

3. The Web container obtains the URI attribute value of the taglib declared in the first step, based on the prefix in the second step

4. The Web container finds the corresponding element in web.xml based on the URI property

5. Gets the value of the corresponding element from the element

6. The Web container finds the corresponding. tld file from the web-inf/directory based on the value of the element

7. Find the element corresponding to tagname from the. tld file

8. To get the value of the corresponding element in a rounding element

9. The Web container creates an instance of the corresponding tag handle class based on the value of the element

The Web container calls the Dostarttag/doendtag method of this instance to complete the corresponding processing

The basic steps to create and use a tag library:

1. Create label processing class (Tag Handler Class)

2. Create Tag library description file (tag Descrptor file)

3. Configure elements in the Web.xml file

4. Attracting tag libraries in JSP files

Five, TagSupport class introduction:

1. The class that handles the label must extend the Javax.servlet.jsp.TagSupport.

Main properties of the 2.TagSupport class:

A.parent Property: A processing class that represents an upper-level label that has nested current labels

B.pagecontex property: Represents a Javax.servlet.jsp.PageContext object in a Web application

The 3.JSP container invokes the Setpagecontext and SetParent methods before invoking the doStartTag or Doendtag method, setting PageContext and parent. So you can access PageContext variables directly in the label processing class

4. PageContext member variables cannot be accessed in the TagSupport construction method because the JSP container has not yet called

The Setpagecontext method initializes the PageContext

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.