Detailed description of JSP custom tags from the shortest to the deep dive (1)

Source: Internet
Author: User
Tags tld

I. Basic Concepts

1. Tag)

A tag is an XML element. A tag can simplify and maintain a JSP webpage, and supports multiple language versions for the same JSP file. Because tags are XML elements, their names and attributes are case sensitive.

2. Tag library)

A set of tags that have similar functions and are logically associated with each other is called a tag library.

3. Tag Library description file (Tag Library Descriptor)

The tag library description file is an XML file that provides the ing between the class in the tag Library and the tag reference in JSP. It is a configuration file, which is similar to web. xml.

4. Tag Handle Class)

The tag processing class is a Java class that inherits the TagSupport or extends the SimpleTag interface. This class can be used to customize the specific functions of JSP labels.

Ii. Custom JSP tag format

1. To enable the JSP Container to use the custom behavior in the tag library, the following two conditions must be met:

<% @ taglib prefix=”someprefix” uri=”/sometaglib” %>

1) Identify the label representing the custom behavior from a specified tag library;
2) Find the specific class to implement these custom rows.

The first required condition-Identify the tag library that a custom behavior belongs to-is completed by the Taglib Directive's Prefix attribute of the tag command, therefore, all elements with the same prefix on the same page belong to this tag library. Each tag library defines a default prefix, which is used to insert custom tags in the document or page of the tag library. Therefore, you can use prefixes such as jsp, jspx, java, servlet, sun, and sunw (they are all reserved words specified in the JSP White Paper.

The uri property meets the second requirement. Find the corresponding class for each custom behavior. This uri contains a string that the container uses to locate the TLD file. In the TLD file, you can find the names of all tag processing classes in the tag library.

2. When the web application starts, the container searches for all files ending with. tld from the WEB-INF of the directory structure of the META-INF folder. That is to say, they will locate all TLD files. For each TLD file, the container first obtains the URI of the tag library, and then creates a ing relationship between each TLD file and the corresponding URI.

On the JSP page, we only need to use the tag library command with the URI property value to match the specific tag library.

3. Process custom JSP tags

1. Introduce the tag library in JSP

<% @ taglib prefix=”taglibprefix” uri=”tagliburi” %>

2. Use tag library tags in JSP

3. The Web Container obtains the uri attribute value of taglib declared in the first step based on the prefix in the second step.

4. Web containers are stored in the web. xml to find the corresponding element 5. obtain the value of the corresponding element from the element 6. the Web Container finds the corresponding under the WEB-INF/directory based on the element value. tld file 7. from. find the element corresponding to the tagname In the tld file. get the value of the corresponding element in the elements. 9. the Web Container creates an instance of the corresponding tag handle class based on the value of the element 10. the Web Container calls the doStartTag/doEndTag method of this instance to complete corresponding processing.

4. Basic Steps for creating and using a Tag Library

1. Tag Handler Class)

2. Create a Tag Library description File (Tag Library Descrptor File)

3. Configure elements in the web. xml file 4. Introduce the tag library in the JSP file

V. Introduction to TagSupport

1. The class for processing tags must extend javax. servlet. jsp. TagSupport.

2. Main attributes of the TagSupport class:

A. parent attribute: indicates the processing class of the Upper-layer labels nested with the current tag;

B. pageContex attribute: indicates the javax. servlet. jsp. PageContext object in the Web application;

3. before calling the doStartTag or doEndTag method, JSP containers call the setPageContext and setParent methods to set pageContext and parent. Therefore, you can directly access the pageContext variable in the tag processing class;

4. The pageContext member variable cannot be accessed in the TagSupport constructor, because the JSP Container has not yet called the setPageContext method to initialize 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.