Contents | Prev | Next



Types of Tags

JSP custom actions are expressed using XML syntax. They have a start tag and end tag, and possibly a body:

<tlt:tag>
   body
</tlt:tag>
A tag with no body can be expressed as follows:

<tlt:tag />

Simple Tags

The following simple tag invokes an action that creates a greeting:

<tlt:greeting />

Tags With Attributes

The start tag of a custom action can contain attributes in the form 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.

Table 1 Valid Tag Attribute Assignments

Property Type

Conversion on String Value

boolean or Boolean

As indicated in java.lang.Boolean.valueOf(String)

byte or Byte

As indicated in java.lang.Byte.valueOf(String)

char or Character

As indicated in java.lang.Character.valueOf(String)

double or Double

As indicated in java.lang.Double.valueOf(String)

int or Integer

As indicated in java.lang.Integer.valueOf(String)

float or Float

As indicated in java.lang.Float.valueOf(String)

long or Long

As indicated in java.lang.Long.valueOf(String)

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 %>" />

Tags With a Body

A tag can contain custom and core tags, scripting elements, HTML text, and tag-dependent body content between the start and end tag. In the following example, the date information is provided in the body of the tag, instead of as an attribute:

<tlt:greeting>
   <%= today %>
</tlt:greeting>

Choosing Between Passing Information as Attributes or Body

As shown in the last two sections, it is possible to pass a given piece of data as an attribute of the tag or to the tag's body. Generally speaking, any data that is a simple string or can be generated by evaluating a simple expression is best passed as an attribute.


Tags That Define Scripting Variables

A tag can define a variable that can be used in scripts within a page. The following example illustrates how to define and use a scripting variable that contains an object returned from a JNDI lookup. Examples of such objects include enterprise beans, transactions, databases, environment entries, and so on:

<tlt:lookup id="tx" type="UserTransaction" 
   name="java:comp/UserTransaction" />
<% tx.begin(); %>

Cooperating Tags

Tags cooperate with each other by means of shared objects.

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.

<tlt:tag1 id="obj1" attr2="value" />
<tlt:tag2 name="obj1" />
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:outerTag>
   <tlt:innerTag />
</tlt:outerTag>


Contents | Prev | Next
Copyright © 2000 Sun Microsystems, Inc. All rights reserved.