Struts: Development of Custom Taglib

Source: Internet
Author: User
Tags extend tld

The custom label must implement one of the following three interfaces: Tag, Iterationtag, Bodytag

1.Tag

If you want to implement this interface, you can extend the TagSupport class to write the method you need without having to implement all the methods in the tag interface.

Methods of the Tag interface: doStartTag (), Doendtag (), GetParent (), SetParent (), Release (), Setpagecontext ()

In the tag class code can not be like a JSP, directly using out hidden objects, he has an object can use PageContext, through its getout () method can get out objects. Inside the tag, any implied objects are accessed by calling the PageContext set method.

2.IterationTag

The Iterationtag interface is similar to the tag interface and is used when a custom tag needs to compute its code body repeatedly. It expands the tag interface and implements a new method Doafterbody () to implement the loop, which is invoked only when the doStartTag () is returned to Eval_body_include. When the Doafterbody () method is executed, if the return is Eval_body_again, 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 ability to manipulate the content of the code body. It is when the body of the code is computed that the body of the code that has been formed can be modified. The Bodycontent object is the result that is used to save the calculation of the custom tag body. It has a new method Doinitbody (), which is invoked only when the doStartTag () method returns eval_body_buffered, where it creates a Bodycontent object to save the result.

To extend a custom label:

Add properties

First you add an attribute element to the TLD file, and then you need to define the attribute and its setter method in the Java file. Attributes <attribute> elements have four child elements are <name>, <required>, <rtexprvalue>, <description> Rtexprvalue> represents whether the property accepts the evaluation of a scriptlet expression, or false by default, that is, only static values can be accepted.

Add variable

You can add a <variable> element to a custom tag in a TLD file, and its child elements include <name-given> to hold the variable's name,<variable-class> the Java type that represents the variable. <declared> Boolean Indicates whether this variable is a new,<scope> indicating the scope of use of the variable (At_begin represents from the start tag, at_end from the end tag, Nested indicates between the start and end tags). Once you have defined the variable, you need to use the variable Pagecontext.setattribute ("", object) in the Java file, where the key value should be the variable's name.

Using the Tagextrainfo (TEI) class

There are two types of objects in this object that can be used, tagdata (the information that holds the label attribute), Variableinfo (description code variable)

A section of Tagextrainfo Class code example:

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;
  }

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.