Preface

Comment

Declaration

Expression

Scriptlet

EL Expression

Directives

Attribute Directive

Include Directive

Page Directive

Tag Directive

Taglib Directive

Variable Directive

Standard Actions

<jsp:attribute>

<jsp:body>

<jsp:element>

<jsp:doBody>

<jsp:forward>

<jsp:getProperty>

<jsp:include>

<jsp:invoke>

<jsp:output>

<jsp:plugin>

<jsp:root>

<jsp:setProperty>

<jsp:text>

<jsp:useBean>

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" ]	
%>

OR

<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.

Here is another example:

<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

See Also



宏飞网络是你学习web开发、测试web程序实例、和培养职业技能的首选网站。我们提供例子也许有些简单,但对理解基本概念有帮助。

我们尽量避免在教程、参考及例子中出现错误,但不能保证所有的内容都是正确的。

你使用本网站时,我们默认你已经阅读并接受了我们的隐私政策。

Copyright 2003-2011宏飞网络 版权所有