Custom tags in Jsp are explained in detail from the shortest to the deep test (1)

Source: Internet
Author: User

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:

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

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

1. Identify the tag representing this custom row from a specified tag Library

2. Find the specific class to implement the custom Behavior

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.

<someprefix:sometag key=”somevalue”/> 

When a 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 labels:

1. Introduce the tag library in JSP:

2. Use the tag library tag 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. The Web Container finds the corresponding element in web. xml based on the uri attribute.

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

6. Web containers find the corresponding. tld file from the WEB-INF/directory based on the element value

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

8. Obtain the value of the corresponding element from the elements.

9. The Web Container creates the corresponding tag handle class instance based on the element value.

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. Add a tag Library to 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 called

The setPageContext method initializes 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.