background image

Variable Synchronization Behavior

<< Variable Directive Attributes | NESTED Scope >>
<< Variable Directive Attributes | NESTED Scope >>

Variable Synchronization Behavior

TABLE 8­5
Variable Synchronization Behavior
Tag File Location
AT_BEGIN
NESTED
AT_END
Beginning
Not sync.
Save
Not sync.
Before any fragment invocation using
jsp:invoke
or jsp:doBody (see
"Evaluating
Fragments Passed to Tag Files" on page 242
)
Tag
page
Tag
page
Not sync.
End
Tag
page
Restore
Tag
page
If name-given is used to specify the variable name, then the name of the variable in the calling
page and the name of the variable in the tag file are the same and are equal to the value of
name-given
.
The name-from-attribute and alias attributes of the variable directive can be used to
customize the name of the variable in the calling page while another name is used in the tag file.
When using these attributes, you set the name of the variable in the calling page from the value
of name-from-attribute at the time the tag was called. The name of the corresponding variable
in the tag file is the value of alias.
Synchronization Examples
The following examples illustrate how variable synchronization works between a tag file and its
calling page. All the example JSP pages and tag files reference the JSTL core tag library with the
prefix c. The JSP pages reference a tag file located in /WEB-INF/tags with the prefix my.
AT_BEGIN
Scope
In this example, the AT_BEGIN scope is used to pass
the value of the variable named x to the tag's body
and at the end of the tag invocation.
<%-- callingpage.jsp --%>
<c:set var=
"x" value="1"/>
${x} <%-- (x == 1) --%>
<my:example>
${x} <%-- (x == 2) --%>
</my:example>
${x} <%-- (x == 4) --%>
<%-- example.tag --%>
<%@ variable name-given=
"x" scope="AT_BEGIN" %>
${x} <%-- (x == null) --%>
<c:set var=
"x" value="2"/>
<jsp:doBody/>
${x} <%-- (x == 2) --%>
<c:set var=
"x" value="4"/>
Encapsulating Reusable Content Using Tag Files
The Java EE 5 Tutorial · September 2007
240