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.
<% @ 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 label representing this custom row 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 a web applicationProgramAt startup, 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:
<% @ 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. 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.