Declares an expression language variable exposed by the tag to the calling page.
<%@ 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 />
<jsp:directive.variable variableDirectiveAttrList />
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.
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.
name-given="scripting variable" | name-from-attribute="scripting variable"
dynamic-attributes attribute of a tag directive.
alias="locally-scoped attribute" name-from-attribute.
name-from-attribute is specified. A translation error results if used without name-from-attribute.
alias is the same as the value of a name attribute of an attribute directive or the name-given attribute of a variable directive.
variable-class="java.lang.String" | name of the variable class"declare="true | false"scope="AT_BEGIN | AT_END | NESTED"description="text"