background image

Tags That Define Variables

<< The BodyTagSupport Class | JavaServer Faces Technology >>
<< The BodyTagSupport Class | JavaServer Faces Technology >>

Tags That Define Variables

<tag>
...
<attribute>
<name>connectionId</name>
<required>false</required>
</attribute>
</tag>
Tags That Define Variables
The mechanisms for defining variables in classic tags are similar to those described in
Chapter 8, "Custom Tags in JSP Pages."
You must declare the variable in a variable element of
the TLD or in a tag extra info class. Use PageContext().setAttribute(name,value) or
PageContext.setAttribute(name,value,scope)
methods in the tag handler to create or
update an association between a name that is accessible in the page context and the object that is
the value of the variable. For classic tag handlers,
Table 9­3
illustrates how the availability of a
variable affects when you may want to set or update the variable's value.
TABLE 9­3
Variable Availability
Value
Availability
In Methods
NESTED
Between the start tag and the end tag
doStartTag
, doInitBody, and doAfterBody
AT_BEGIN
From the start tag until the end of the page
doStartTag
, doInitBody, doAfterBody, and
doEndTag
AT_END
After the end tag until the end of the page
doEndTag
A variable defined by a custom tag can also be accessed in a scripting expression. For example,
the web service described in the preceding section can be encapsulated in a custom tag that
returns the response in a variable named by the var attribute, and then var can be accessed in a
scripting expression as follows:
<ws:hello var=
"response"
name=
"<%=request.getParameter("username")%>" />
<h2><font color=
"black"><%= response %>!</font></h2>
Remember that in situations where scripting is not allowed (in a tag body where the
body-content
is declared as scriptless and in a page where scripting is specified to be
invalid), you wouldn't be able to access the variable in a scriptlet or an expression. Instead, you
would have to use the JSP expression language to access the variable.
Programming Tags That Accept Scripting Elements
The Java EE 5 Tutorial · September 2007
284