background image

NESTED Scope

<< Variable Synchronization Behavior | Evaluating Fragments Passed to Tag Files >>
<< Variable Synchronization Behavior | Evaluating Fragments Passed to Tag Files >>

NESTED Scope

NESTED
Scope
In this example, the NESTED scope is used to make a
variable named x available only to the tag's body. The
tag sets the variable to 2, and this value is passed to
the calling page before the body is invoked. Because
the scope is NESTED and because the calling page also
had a variable named x, its original value, 1, is
restored when the tag completes.
<%-- callingpage.jsp --%>
<c:set var=
"x" value="1"/>
${x} <%-- (x == 1) --%>
<my:example>
${x} <%-- (x == 2) --%>
</my:example>
${x} <%-- (x == 1) --%>
<%-- example.tag --%>
<%@ variable name-given=
"x" scope="NESTED" %>
${x} <%-- (x == null) --%>
<c:set var=
"x" value="2"/>
<jsp:doBody/>
${x} <%-- (x == 2) --%>
<c:set var=
"x" value="4"/>
AT_END
Scope
In this example, the AT_END scope is used to return a
value to the page. The body of the tag is not affected.
<%-- callingpage.jsp --%>
<c:set var=
"x" value="1"/>
${x} <%-- (x == 1) --%>
<my:example>
${x} <%-- (x == 1) --%>
</my:example>
${x} <%-- (x == 4) --%>
<%-- example.tag --%>
<%@ variable name-given=
"x" scope="AT_END" %>
${x} <%-- (x == null) --%>
<c:set var=
"x" value="2"/>
<jsp:doBody/>
${x} <%-- (x == 2) --%>
<c:set var=
"x" value="4"/>
AT_BEGIN
and name-from-attribute
In this example the AT_BEGIN scope is used to pass an
EL variable to the tag's body and make to it available
to the calling page at the end of the tag invocation.
The name of the variable is specified by the value of
the attribute var. The variable is referenced by a local
name, result, in the tag file.
Encapsulating Reusable Content Using Tag Files
Chapter 8 · Custom Tags in JSP Pages
241