| Contents | Prev | Next |
|
A tag with no body can be expressed as follows:<tlt:tag> body </tlt:tag>
<tlt:tag />
<tlt:greeting />
attr="value". Attributes serve to customize the behavior of a tag just as parameters are used to affect the outcome of executing a method on an object.
Tag attributes can be set from one or more parameters in the request object or from a String constant. The only types of attributes that can be set from request parameter values and String constants are those listed in Table 1; the conversion applied is that shown in the table. When assigning values to indexed attributes the value must be an array; the rules just described apply to the elements.
An attribute value of the form <%= scriptlet_expression %> is computed at request time. The value of the expression depends on the type of the attribute's value, which is specified in the object that implements the tag (called a tag handler). Request-time expressions can be assigned to attributes of any type; no automatic conversions will be performed.
The following tag has an attribute named date, which accepts a String value obtained by evaluating the variable today:
<tlt:greeting date="<%= today %>" />
<tlt:greeting> <%= today %> </tlt:greeting>
<tlt:lookup id="tx" type="UserTransaction" name="java:comp/UserTransaction" /> <% tx.begin(); %>
In the following example, tag1 creates a named object called obj1, which is then reused by tag2. The convention encouraged by the JSP specification is that a tag with attribute named id creates and names an object and the object is then referenced by other tags with an attribute named name.
In the next example, an object created by the enclosing tag of a group of nested tags is available to all inner tags. Since the object is not named, the potential for naming conflicts is reduced. The following example illustrates how a set of cooperating nested tags would appear in a JSP page.<tlt:tag1 id="obj1" attr2="value" /> <tlt:tag2 name="obj1" />
<tlt:outerTag> <tlt:innerTag /> </tlt:outerTag>