JSP custom tag ---- & gt; Summary of the first season

Source: Internet
Author: User
Tags tld

1. Overview of custom JSP labels
A custom JSP tag is a type of JSP tag defined by the programmer. This tag encapsulates the information display logic in a separate Java class and describes its usage through an XML file. When you need to use similar display logic in the page, you can insert this label in the page to complete the corresponding functions. Custom tags can be used to separate program logic and representation logic, and Java code is stripped from HTML to facilitate the maintenance of the page by the artist. Custom tags also provide reusable functional components, it can improve the development efficiency of the project.
Custom tags are mainly used to remove java code from Jsp pages.
2. Custom JSP tag Execution Process
When a JSP page containing custom tags is translated into Servlet by the JSP Engine (Web Container), the JSP Engine encounters custom tags, this custom tag is converted into a call called "tag processing class. Then, when the JSP page is executed, the JSP Engine will call the "tag processing class" object and execute the corresponding operation methods defined internally to complete the corresponding functions.
3. Custom JSP tag Development Process
When using the Java processing class to develop custom JSP labels, there are mainly the following steps.
-Tag Handle Class ). This class implements the Tag interface, which is used to define the behavior of tags and is called and executed when the JSP Engine encounters a custom Tag.
-Create a Tag Library description File (tld) and describe the Tag processor class in the tld File.
-Use the taglib command in the JSP file to introduce the tag library, and then use the tag name specified in the tag library description file.
Execution Process of 3Tog custom Interface
When the JSP Engine encounters a custom tag, it first creates the Instance Object of the tag processor class, and then calls its method according to the communication rules defined in the JSP specification.
1) public void setPageContext (PageContext pc). After the JSP Engine instantiates the tag processor, it will call the setPageContext method to pass the pageContext object of the JSP page to the tag processor, the tag processor can communicate with the JSP page through this pageContext object.
2) public void setParent (Tag t). After the setPageContext method is executed, the WEB Container then calls the setParent method to pass the parent Tag of the current Tag to the current Tag processor, if the current tag does not have a parent tag, the parameter value passed to the setParent method is null.
3) public int doStartTag (). After the setPageContext method and setParent method are called, The doStartTag method of the tag processor is called when the WEB Container runs the start tag of the custom tag.
4) public int doEndTag (). After the WEB Container executes the TAG body of the custom tag, it then executes the end tag of the custom tag, the WEB Container calls the doEndTag method of the tag processor.
5) public void release (). Generally, after a WEB Container executes a custom tag, the tag processor will reside in the memory and be another request server until the web application is stopped, the web Container will call the release method.
4. JSP Interface
• Common interfaces in JSP 1.1 and 1.2 specifications include the following three.
-Tag: this interface defines the methods to be implemented for all Tag processing classes.
-IterationTag: this interface extends the Tag interface and adds a method to control the repeated execution of Tag subjects.
-BodyTag: this interface extends the IterationTag interface and adds methods for accessing and operating the label subject content.
5. Tag library Descriptor
The tag library descriptor file is a standard XML document ending with ". tld". It is used to record the tags in a tag Library and the attributes of each tag.
The following is the content of a JSP 2.0 standard tag library description file: <taglib> the element is the root element of the tag library descriptor, which contains 12 sub-elements. The details are as follows, (bold elements are common elements, remember ).
-(1) <description>: a text description of the tag library.
-(2) <tlib-version>: Specifies the version of the tag library.
-(3) <short-name>: defines a brief name for the tag, which can be used as the preferred prefix name in the taglib command.
-(4) <uri>: defines a URI that uniquely identifies the tag library.
-(5) <tag>: used to specify information about custom tags.
-(6) <display-name>: Specifies a brief alias for the tag library.
-(7) <small-icon>: Specifies a 16 × 16 small icon (in gif or jpeg format) for the tag library. This icon can be displayed in the graphic interface tool.
-(8) <large-icon>: specify a 32 × 32 icon (in gif or jpeg format) for the tag library. This icon can be displayed in the graphic interface tool.
-(9) <validator>: provides a validator for the tag library.
-(10) <listener>: provides a listener for the tag library.
-(11) <tag-file>: used to describe the tag file.
-(12) <function>: used to specify the function used in the expression language.
6. Development of Traditional labels
Before developing traditional tags, you need to understand the lifecycle of the two support classes (TagSupport and BodyTagSupport.
The lifecycle of the TagSupport class can be expressed in graphs.

The lifecycle of the BodyTagSupport class can be expressed in graphs.

7. Use the BodyTagSupport class to develop custom tags
• Two methods are added to the BodyTagSupport class:
-SetBodyContent (BodyContent bc): When the container executes this label processing class instance, it will call this method and cache the content returned by the label body in the BodyContent class instance. In addition to inheriting from the parent class JspWriter, BodyContent provides methods for writing text to the response body, and also provides the label body content for obtaining the buffer.
-DoInitBody (): Call this method before calculating the label subject for initialization. You can write the initial content to the bodyContent object, which is placed before the label body content.
• The BodyContent class is used to cache the content returned by the label body, including static text and dynamic content created by nested labels or script elements.
The role or application of traditional custom tags:
-Whether to execute a part of the jsp page.
-Determines whether the entire jsp page is executed.
-Control repeated execution of jsp page content.
-Modify jsp page content output.
• In addition to java code removed from the jsp page, the custom tag can also implement the above functions.
• Four tag types in tld files
-Empty jsp scriptless tagdepentend

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.