- 7.1 Tag Library Components
- 7.2 Example: Simple Prime Tag
- 7.3 Assigning Attributes to Tags
- 7.4 Example: Prime Tag with Variable Length
- 7.5 Including Tag Body in the Tag Output
- 7.6 Example: Heading Tag
- 7.7 Example: Debug Tag
- 7.8 Creating Tag Files
- 7.9 Example: Simple Prime Tag Using Tag Files
- 7.10 Example: Prime Tag with Variable Length Using Tag Files
- 7.11 Example: Heading Tag Using Tag Files
7.3 Assigning Attributes to Tags
Allowing tags like
<prefix:name attribute1="value1" attribute2="value2"... />
adds significant flexibility to your tag library because the attributes allow us to pass information to the tag. This section explains how to add attribute support to your tags.
Tag Attributes: Tag Handler Class
Providing support for attributes is straightforward. Use of an attribute called attribute1 simply results in a call to a method called setAttribute1 in your class that extends SimpleTagSupport (or that otherwise implements the SimpleTag interface). Consequently, adding support for an attribute named attribute1 is merely a matter of implementing the following method in your tag handler class:
public void setAttribute1(String value1) { doSomethingWith(value1); }
Note that an attribute with the name of attributeName (lowercase a) corresponds to a method called setAttributeName (uppercase A).
One of the most common things to do in the attribute handler is to simply store the attribute in a field for later use by the doTag method. For example, the following is a code snippet of a tag implementation that adds support for the message attribute:
private String message = "Default Message"; public void setMessage(String message) { this.message = message; }
If the tag handler is accessed from other classes, it is a good idea to provide a getAttributeName method in addition to the setAttributeName method. Only setAttributeName is required, however.
Tag Attributes: Tag Library Descriptor
Tag attributes must be declared inside the tag element by means of an attribute element. The attribute element has three nested elements that can appear between <attribute> and </attribute>.
- name. This is a required element that defines the case-sensitive attribute name.
- required. This is an optional element that stipulates whether the attribute must always be supplied, true, or is optional, false (default). If required is false and the JSP page omits the attribute, no call is made to the setAttributeName method, so be sure to give default values to the fields that the method sets if the attribute is not declared as required. Omitting a tag attribute, which is declared with the required element equal to true, results in an error at page translation time.
- rtexprvalue. This is an optional element that indicates whether the attribute value can be either a JSP scripting expression like <%= expression %> or JSP EL like ${bean.value} (true), or whether it must be a fixed string (false). The default value is false, so this element is usually omitted except when you want to allow attributes to have values determined at request time. Note that even though it is never legal for the body of the tag to contain JSP scripting expressions like <%= expression %>, they are nevertheless legal as attribute values.
Tag Attributes: JSP File
As before, the JSP page has to declare the tag library using the taglib directive. This is done in the following form:
<%@ taglib uri="..." prefix="..." %>
The usage of the tag is very similar, except now we are able to specify a custom attribute as well. Remember that just like tag names, the attribute names are case-sensitive and have to appear in the JSP page exactly as they were declared inside the TLD file. Because custom tags are based on XML syntax, the value of an attribute has to be enclosed by either single or double quotes. For example:
<some-prefix:tag1 attribute1="value" />