Variable Directive
Declares an expression language variable exposed by the tag to the calling page.
JSP Syntax
<%@ variable { name-given="scripting variable" |
(name-from-attribute="scripting variable"
alias="locally-scoped attribute")}
[ variable-class="java.lang.String" | name of the variable class" ]
[ declare="true | false" ]
[ scope="AT_BEGIN | AT_END | NESTED" ]
[ description="text" ]
%>
<jsp:directive.variable variableDirectiveAttrList />
XML Syntax
<jsp:directive.variable variableDirectiveAttrList />
Examples
The following tag file declares a variable, x, with a scope of AT_END and sets its value to 3.
<%-- a.tag --%> <%@ variable name-given="x" scope="AT_END" %> <c:set var="x" value="3" />
The following JSP page references the variable with an EL expression.
<%-- b.jsp --%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
${x}
<tags:a />
${x}
The first reference to the variable is undefined because the variable has not been declared yet. After the tag file has been invoked and the variable is set, the page references the variable again, which will result in an output of 3.
Description
The variable directive is used to declare variables in tag files. The variable directive is analogous to the variable element in the Tag Library descriptor, and defines the details of a variable exposed by the tag handler to the calling page.
A custom tag (whether it's implemented using Java or a tag file) is able to declare that it returns variables to the calling page. For example, the following tag can look up user information and place it in a bean:
<mytag:lookupUserInfo />
${userInfo.name}
In this case, the lookupUserInfo tag can declare that it returns a variable to the calling page with a name of userInfo.
<mytag:displayCustomers>
${customer.name}
</mytag:displayCustomers>
The scope of the variable affects how it is exposed to the calling page (AT_BEGIN means the variable is available from the start tag onwards, AT_END means the variable is available from the end tag onwards, and NESTED means the variable is only available in the body of the tag). In the first preceding example, you would probably use AT_END so that the variable is still available at the end of the tag. In the second example, you would probably use NESTED since the ${customer} variable doesn't need to be accessible anywhere but inside the displayCustomers tag.
When you want to emulate OUT parameters, use variables with scope AT_BEGIN or AT_END. For each AT_BEGIN or AT_END variable, a page-scoped attribute is made available in the JspContext of the tag file. The scoped attribute is not initialized. Synchronization is performed at the end of the tag for AT_BEGIN and AT_END and also before the invocation of a fragment for AT_BEGIN.
When you want to emulate nested parameters, use variables with scope AT_BEGIN or NESTED. For each AT_BEGIN or NESTED variable, a page-scoped attribute is made available in the JspContext of the tag file. The scoped attribute is not initialized. Synchronization is performed before each fragment invocation for AT_BEGIN and NESTED, and also after the end of the tag for AT_BEGIN
The JSP specification recommends that to accomplish IN parameters, use attributes. To accomplish OUT or NESTED parameters, use variables.
Attributes
name-given="scripting variable" | name-from-attribute="scripting variable"- Defines an EL variable to be used in the page invoking this tag. Either name-given or name-from-attribute must be specified. If name-given is specified, the value is the name of an attribute whose (translation-time) value at the start of the tag invocation will give the name of the variable.
- Translation errors arise in the following circumstances:
- Translation errors arise in the following circumstances:
- Specifying neither name-given nor name-from-attribute.
- If two variable directives have the same name-given.
- If the value of a name-given attribute of a variable directive is equal to the value of a name attribute of an attribute directive or the value of a
dynamic-attributesattribute of a tag directive.
- Defines an EL variable to be used in the page invoking this tag. Either name-given or name-from-attribute must be specified. If name-given is specified, the value is the name of an attribute whose (translation-time) value at the start of the tag invocation will give the name of the variable.
alias="locally-scoped attribute"
- Defines a variable, local to the tag file, to hold the value of the EL variable. The container will synchronize this value with the variable whose name is given in
name-from-attribute.- Required when
name-from-attributeis specified. A translation error results if used withoutname-from-attribute.- A translation error results if the value of
aliasis the same as the value of anameattribute of anattributedirective or thename-givenattribute of avariabledirective. - Required when
- Defines a variable, local to the tag file, to hold the value of the EL variable. The container will synchronize this value with the variable whose name is given in
variable-class="java.lang.String" |name of the variable class"
declare="true| false"
scope="AT_BEGIN | AT_END |NESTED"
description="text"
