Custom struts labels

Source: Internet
Author: User
Tags finally block tld

Custom tags must implement one of the following three interfaces: Tag, iterationtag, and bodytag.
1. Tag
To implement this interface, you can extend the tagsupport class to write the methods you need without implementing all the methods in the tag interface.
Tag interface method:
Dostarttag (), doendtag (), getparent (), setparent (), release (), setpagecontext ()
In tag code, the out implicit object cannot be used directly like JSP. One of the objects can use pagecontext and the out object can be obtained through its getout () method. To access any hidden object within the tag, call the Set Method of pagecontext.

2. iterationtag
The iterationtag interface is similar to the tag interface. It is used when a custom tag needs to calculate its code body repeatedly. It extends the tag interface and implements a new method doafterbody () to implement loops. This method is called only when dostarttag () returns eval_body_include. When the doafterbody () method is executed, if eval_body_again is returned, the doafterbody () method is executed again until doafterbody () returns skip_body or eval_body_include.

3. bodytag
The bodytag interface extends the iterationtag and provides the function of operating the code body content. When calculating the code body, you can modify the formed code body. The bodycontent object is used to save the computing results of the custom tag body. It has a new method, doinitbody (), which is called only when the dostarttag () method returns eval_body_buffered. At this time, it creates a bodycontent object to save the result.

Extended custom tags:
Add attribute
First, add an attribute element to the TLD file, and then define this attribute and Its setter method in the Java file. The attribute <attribute> element has four sub-elements: <Name>, <required>, <rtexprvalue>, and <description>, <rtexprvalue> indicates whether the attribute accepts the computation result of the scriptlet expression. By default, the value is false, that is, the attribute can only accept static values.

Add variable
You can add a <variable> element to a custom tag in a TLD file. Its sub-elements include <name-given>, <variable-class> indicates the Java type of the variable. <declared> Boolean indicates whether the variable is new. <scope> indicates the use range of the variable (at_begin indicates that the start label starts, at_end indicates that after the label is terminated, nested indicates between the start label and the end label ). After the variable is defined, you need to use pagecontext. setattribute ("", object) in the Java file. The key value here should be the external name of the variable.

Use the tagextrainfo (TEI) Class
This object can be used by two types of objects: tagdata (storing tag attributes) and variableinfo (describing code variables)
Example of a piece of tagextrainfo code:
Public variableinfo [] getvariableinfo (tagdata data ){
String variablename = data. getattributestring ("name ");
Variableinfo Vi =
New variableinfo (variablename, "string []", true, variableinfo. at_end );
Variableinfo [] tagvariables = new variableinfo [1];
Tagvariables [0] = VI;
Return tagvariables;
}
You can use the getattributestring method of the tagdata class to obtain the value of an attribute. Another method, getattribute, also obtains the value of an attribute, but returns an object. The getvariableinfo method must return a variableinfo array. In addition, you also need to add a <Tei-class> element after the Element Definition <tag-class> In the TLD, indicating the full name of the TEI class.

The pagecontext object contains the following methods: getout (); getpage (); getrequest (); getresponse (); getservletconfig (); getservletcontext (); getsession ();

The return constant in the tag interface indicates:
Eval_body_include: tells the server body content and sends the content to the output stream.
Skip_body: tells the server not to process the body content
Eval_page: allows the server to continue executing the page
Skip_page: prevents the server from processing the remaining page
Eval_body_again: Let the server continue to process the body content. Only the doafterbody method can return
Eval_body_buffered: Field of the bodytag interface, which is returned in dostarttag ().
Eval_body_include and skip_body are generally returned by dostarttag (), while eval_papge and skip_page are returned by doendtag.

Before calling the dostarttag () method, the tag actually calls two other methods: setpagecontext () and setparent (). Therefore, you can use pagecontext and parent objects in subsequent methods, if necessary.

The custom tag must use a standard JSP object tagextrainfo class when creating an object on the page. It can create script variables and test tags during compilation, the Tei class can only generate variables stored in the pagecontext object by the setattribute method, instead of generating variables separately.

Using the TEI class definition script variable, you can customize the object name on the page.
In addition to the TEI method, you can also define a <variable> object in the TLD to use the custom object. The usage is as follows:
<Variable>
<Name-from-attribute> name </name-from-attribute>
<Variable-class> string [] </variable-class>
<Declare> true </declare>
<Scope> at_end </scope>
</Variable>
For the child element of variable, <name-from-attribute> indicates that the created variable name is obtained from the attribute name, you can also use the <name-given> element to limit the variable name. Note that these two elements are mutually exclusive.

The lifecycle of a custom extension bodytagsupport tag is as follows:
1. Create a tag
2. Call the setter Method
3. Call the dostarttag () method
4. Call the setbodycontent () method
5. Call the initbody () method
6. process the marked body
7. doafterbody (); Based on the returned value, if it is eval_body_again, continue to execute 6. If not, execute 8
8. Call the doendtag () method
9. Determine whether the tag needs to be reused. If yes, execute 4; otherwise, execute the release () method.

The findancestorwithclass () method of the tagsupport class can be used to find the specified parent class. It has two parameters: the class name and the parent class name to be searched, if no null is returned, for example, parenttag parent = (parenttag) This. findancestorwithclass (this, parenttag. class );

Authentication Method for custom tags:
Jsp1.1
The Tei class can check its own tag during compilation. This class has an isvalid () method. If the TEI class is defined for this tag in the TLD, the webpage will call this method during compilation and pass in a tagdata parameter containing the specific content of the attribute. (Also valid in jsp1.2)
Jsp1.2
In jsp1.2, a new tag validation method is introduced to define a new taglibraryvalidator class, and the class of the validation flag can be derived from this method. In most cases, only the validate () method of this class is used, it has three parameters: prefix (prefix defined in the taglib command), Uri (URI in the TLD file), page (pagedata XML version of the JSP page), and validate () if the return value is null, the verification is successful. Otherwise, the returned string type is an error message.
When validator is defined in a TLD file, it should be placed outside the <tag> element definition because it is used to process all the tags in the validation tag library.
<Validator> <validator-class> </validator>.

Compare the methods in jsp1.2 and jsp1.1:
Taglibraryvalidator is more comprehensive than the TEI class and can be used to detect the entire web page, not just the mark itself, but to process the cooperation between tags, and this method can be used to notify programmers where the error is located, but at the same time, its method is much more complicated than the TEI class method, because it needs to traverse the JSP of the entire XML version (complete the getattributevalue method ).

Trycatchfinally interface in jsp1.2:
This interface is mainly used to release resources in the custom tag when the custom tag encounters an exception. It defines two methods:
Public void docatch (throwable T); (this method is called when the dostarttag, doinitbody, doafterbody, and doendtag methods are abnormal)
Piblic void dofinally (); (this method is called no matter whether an exception occurs or not after the doendtag is called, just like the Finally block in the program, which can be used to release resources)

In jsp1.2, you can add an element <URI> </uri> to the TLD file to specify the name used in the taglib command. Then, the TLD file and the manifest. mf is put together in the META-INF directory, so you can easily import these TLD in the page.

Principles for writing custom tags:
1. Use script variables (allow the designer to name the script variables, minimize the number of script variables, and Use Javabean by using a combination of Script objects and access functions)
2. Try to avoid creating a new set of languages when designing collaborative markup, and use script variables whenever possible.
3. write code instead of content. do not generate HTML in custom tags, which will lead to a loss of universality.

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.