Java Qiao Xiaosong-simple tag

Source: Internet
Author: User
Tags tld

1. Simple tag
Because the traditional tag uses three tag interfaces to complete different functions, it is too cumbersome and is not conducive to the promotion of tag technology. In order to reduce the difficulty of learning tag technology, SUN, in JSP 2.0, a simpler and easier-to-write and call SimpleTag interface is defined to implement the tag function. The labels that implement the SimpleTag interface are generally called simple labels. Simple labels define five methods:
SetJspContext Method
SetParent and getParent Methods
SetJspBody Method
DoTag Method
SimpleTag Method
SetJspContext Method
It is used to pass the pageContext object of the JSP page to the tag processor object.
SetParent Method
Used to pass the parent Tag Processor object to the current Tag Processor object
GetParent Method
Parent Tag Processor object used to obtain the current tag
SetJspBody Method
It is used to pass the JspFragment object representing the TAG body to the tag processor object.
DoTag method www.2cto.com
It is used to complete all tag logic, including output, iteration, and modification of TAG body content. Javax can be thrown in the doTag method. servlet. jsp. skipPageException indicates that the WEB Container no longer executes the content after the ending Tag in the JSP page, which is equivalent to returning the Tag in the doEndTag method of the traditional Tag. SKIP_PAGE constant.
 
2. execution sequence of SimpleTag interface methods
When the web Container starts to execute tags, the following method is called to complete tag initialization:
The WEB Container calls the setJspContext method of the tag processor object to pass the pageContext object representing the JSP page to the tag processor object.
The WEB Container calls the setParent method of the tag processor object and passes the parent Tag Processor object to the tag processor object. Note that the WEB Container will call this method only when the tag has a parent tag.
If an attribute is set when a tag is called, the container will call the setter method corresponding to each attribute to pass the attribute value to the tag processor object. If the attribute value of a tag is an EL expression or a script expression, the WEB Container first calculates the value of the expression and passes the value to the tag processor object.
If a simple tag has a label body, the container will call the setJspBody method to pass in the JspFragment object representing the label body.
3. When executing tags:
The container calls the doTag () method of the tag processor. by operating the JspFragment object in the method body, developers can execute, iterate, and modify the TAG body.
4. JspFragment class
Javax. servlet. jsp. tagext. the JspFragment class is defined in JSP2.0. Its instance object represents a JSP segment on the JSP page that complies with the JSP syntax specifications. This JSP segment cannot contain JSP script elements.
 
When processing the label body of a simple tag, the WEB Container uses a JspFragment object to represent the content of the TAG body, and calls the setJspBody method of the tag processor object to pass the JspFragment object to the tag processor object. Only two methods are defined in the JspFragment class, as shown below:
 
GetJspContext Method
Return the JspContext object that represents the call page.
 
Public abstract void invoke (java. io. Writer out)
Used to execute the JSP code snippet represented by the JspFragment object
The out parameter is used to specify the output stream object to which the execution result of the JspFragment object is written. If the value passed to the out parameter is null, the execution result is written to JspContext. in the output stream object returned by the getOut () method. (In short, it can be understood as writing to a browser)
5. Detailed description of the invoke Method
JspFragment. the invoke method is the most important method of JspFragment, this method can be used to control whether to execute and output the content of the label body, whether to iteratively execute the content of the label body, or modify the execution result of the label body before outputting. For example:
If the JspFragment. invoke method is not called in the tag processor, the result is equivalent to ignoring the TAG body content;
If the JspFragment. invoke method is repeatedly called in the tag processor, the TAG body content will be repeated;
To modify the content of the TAG body in the tag processor, you only need to specify an output stream object (such as StringWriter) that can retrieve the result data when calling the invoke method ), let the execution result of the TAG body be output to the output stream object. Then, extract the data from the output stream object and modify the data before outputting the data to the target device. This allows you to modify the TAG body.
6. Develop tags with attributes
Custom tags can define one or more attributes. In this way, you can set the values of these attributes when applying custom tags on the JSP page, and pass parameter information for the tag processor through these attributes, this improves the flexibility and reusability of tags.
To make a custom tag have attributes, you usually need to complete two tasks:
Compile the setter method for each attribute in the tag processor.
7. attributes of the scan tag in the TLD File
 
When defining attributes for custom tags, each attribute must follow the JavaBean attribute naming method and define the setter method corresponding to the attribute name in the tag processor, the property value that is passed in when the custom tag is called on the JSP page. For example, the property url defines the corresponding setUrl (String url) method in the tag processor class.
 
After the set method is defined in the tag processor, the JSP Engine calls the set attribute method before parsing and executing the tag, that is, before calling the doStartTag method, to set the attribute for the tag.
 
Describe tag attribute in TLD
 

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.