Experience 12--jsp Simple Tag Introduction __js

Source: Internet
Author: User
Tags tld

1. Simple Label Introduction

Because the traditional label uses three label interface to complete the different function, appears too cumbersome, is not advantageous to the label technology promotion, the sun Company to reduce the label technology learning difficulty, in the JSP 2.0 defines a simpler, facilitates the writing and the invocation Simpletag interface to realize the label function. The label that implements the Simpletag interface is often referred to as a simple label. A simple label altogether defines 5 methods:

Setjspcontext Method

Used to pass the PageContext object of the JSP page to the label processor object

SetParent Method

Used to pass the parent label processor object to the current label processor object

GetParent Method

The parent label processor object used to get the current label

Setjspbody Method

Used to pass the Jspfragment object representing the label body to the label processor object

Dotag Method

Used to complete all the label logic, including output, iteration, modify the contents of the tag body and so on. In the Dotag method, you can throw a Javax.servlet.jsp.SkipPageException exception that notifies the Web container that it no longer executes the content that follows the end tag in the JSP page, which is equivalent to returning Tag.skip in the traditional label's Doendtag method The case of _page constants.

2. Implementation of relevant

order of execution of Simpletag interface methods

When the Web container starts executing the label, the following method is called to complete the initialization of the label

The –web container invokes the Setjspcontext method of the label processor object, passing the PageContext object representing the JSP page to the label processor object.

The –web container invokes the SetParent method of the label processor object, passing the parent label processor object to the label processor object. Note that the Web container will only call this method if the label has a parent tag.

– If a property is set when the label is invoked, the container will call the setter method for each property to pass the property value to the label processor object. If the property value of the label is an El expression or a script expression, the Web container evaluates the value of the expression first, and then passes the value to the label processor object.

– If a simple label has a label body, the container will invoke the Setjspbody method to pass in the Jspfragment object representing the label body.

• When executing the label:

– The container invokes the Dotag () method of the label processor, which enables developers to implement, iterate, and modify the label body by manipulating the Jspfragment object in the method body.

The Javax.servlet.jsp.tagext.jspfragment class is defined in JSP2.0, and its instance object represents a JSP fragment in a JSP page that conforms to the JSP syntax specification, and this JSP fragment cannot contain a JSP script element.

When the Web container handles the label body of a simple label, it represents the contents of the tag body with a Jspfragment object and invokes the Setjspbody method of the label processor object to pass the Jspfragment object to the label processor object. There are only two methods defined in the Jspfragment class, as follows:

Jspfragment class

Getjspcontext method

– Used to return the Jspcontext object that represents the calling page.

public abstract void Invoke (Java.io.Writer out)

The JSP code fragment parameter out that is used to execute the Jspfragment object is used to specify which output stream object to write the result of the Jspfragment object to, and if the value passed to parameter out is NULL, The execution result is written to the output stream object returned by the Jspcontext.getout () method. (In short, it can be understood as addressed to the browser)

The Jspfragment.invoke method is the most important method of jspfragment, which can control whether the content of the label body is executed and output, whether the contents of the execution tag body is iterated or the result of the label body is modified and then output. For example:

– If the Jspfragment.invoke method is not invoked in the label processor, the result is equivalent to ignoring the label body content;

– Repeated calls to the Jspfragment.invoke method in the label processor, the label body content will be repeatedly executed;

– If you want to modify the label body content in the label processor, simply specify an output stream object (such as StringWriter) that can fetch the result data when invoking the Invoke method, so that the result of the label body is output to the output stream object. Then, the data from the output stream object can be modified and then output to the target device to modify the label body.

Attention:

1. After the contents of the label does not output (hidden after the contents of the tag), simple tags and traditional tags a little different, custom hidden tags can not be placed anywhere in the JSP, only in the body tag, while the traditional label can.

2.

3. Development of tagged labels with attributes

A custom label can define one or more properties so that when you apply a custom label to a JSP page, you can set the values of these properties to pass parameter information to the label processor, thereby increasing the flexibility and reusability of the label.

• For a custom label to have attributes, you typically need to accomplish two tasks:

– Write the setter method for each attribute in the label processor

– Attributes of the label in a TLD file

• When defining attributes for a custom label, each attribute must be named in the label processor to define the setter method for the property name, which is used to receive the value of the property passed in when the JSP page calls the custom label. JavaBean For example, a property URL, in the Label processor class, defines the corresponding seturl (String url) method.

• After defining the appropriate set method in the label processor, the JSP engine invokes the Set property method and sets the property for the label before parsing the Execute start tag, that is, before calling the doStartTag method.

describing tag attribute attributes in TLD

element name

Whether you must specify

Description

Description

Whether

Used to specify descriptive information for the property.

name

Is

Used to specify the name of the property. property names are case sensitive and cannot begin with JSPs, _jsp, Java, and Sun.

Required

Whether

used to specify whether this property must be set when calling a custom label in a JSP page . The value includes True and False, the default value is False, and True indicates that the property must be set, or it can be set or not set.

Rtexprvalue

Whether

Rtexprvalue is an English shorthand for runtime expression value (Run-time expression), which specifies that the property value is either a static value or a dynamic value . The value includes true and false, and the default value is false,false to specify static text values for the property, such as "123"; True indicates that a JSP dynamic element can be specified for the property, and the result of the dynamic element is the property value, such as a JSP expression <%= Value%>.

type

Whether

The Java type used to specify the value of the property.

Example:

The <attribute> child elements of a <tag> element are used to describe a custom, a property of a label, and each attribute of a custom label, corresponding to a <attribute> element.

<attribute>

<description>description</description>

<name>aaaa</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

<type>ObjectType</type>

</attribute>

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.