java类库详细讲解
源代码在线查看: attr.html
Saving Data Using JSTL in a JSP Page
(Java Developers Almanac Example)
BODY CODE {font-family: Courier, Monospace; font-size: 11pt} TABLE, BODY {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10pt} PRE {font-family: Courier, Monospace; font-size: 10pt} H3 {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11pt} A.eglink {text-decoration: none} A:hover.eglink {text-decoration: underline} -->
The Java Developers Almanac 1.4
Order this book from Amazon.
Home
>
List of Packages
>
javax.servlet.jsp.jstl.core
[6 examples]
e1066.
Saving Data Using JSTL in a JSP Page
When a JSP page needs to save data for its processing, it must
specify a location, called the scope. See
e1047 Saving Data in a JSP Page for an explanation of the four
available scopes.
Data is saved using a mechanism called scoped variables. A
scoped variable has a name, which is of type String and a value,
which is of type Object. For non-page scoped variables, it is
recommended that the name use the reverse domain name convention
(e.g. prefixed with com_mycompany) to minimize unexpected collisions
when integrating with third party modules.
When using the JSTL's expression language (see
e1064 Enabling the JSTL Expression Language in a JSP Page), the variables in each
scope are made available in the implicit objects - pageScope,
requestScope, sessionScope, and applicationScope.
This example saves and retrieves values in scoped variables in
each of the four scopes:
<%-- Declare the core library --%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%-- Save data in scoped variables --%>
<c:set var="name1" value="value1" scope="page" />
<c:set var="com_mycompany_name2" value="value2" scope="request" />
<c:set var="com_mycompany_name3" value="value3" scope="session" />
<c:set var="com_mycompany_name4" value="value4" scope="application" />
<%-- Show the saved values --%>
<c:out value='${pageScope.name1}' />
<c:out value='${requestScope.com_mycompany_name2}' />
<c:out value='${sessionScope.com_mycompany_name3}' />
<c:out value='${applicationScope.com_mycompany_name4}' />
When retrieving a saved value, it is possible to omit the scope. If
the scope is omitted, the variable name is automatically searched for
in each of the scopes, in the order - pageScope,
requestScope, sessionScope, and applicationScope.
<%-- Show the saved values without a specific scope --%>
<c:out value='${name1}' />
<c:out value='${com_mycompany_name2}' />
<c:out value='${com_mycompany_name3}' />
<c:out value='${com_mycompany_name4}' />
It is also possible to specify the value to save using the contents
of the body, rather than through the value attribute:
<%-- Save data using body content --%>
<c:set var="name1" scope="page">
value 1 in body
</c:set>
<c:set var="com_mycompany_name2" scope="request" >
value 2 in body
</c:set>
<c:set var="com_mycompany_name3" scope="session" >
value 3 in body
</c:set>
<c:set var="com_mycompany_name4" scope="application">
value 4 in body
</c:set>
When specifying the value using body contents, the body contents
is first trimmed of leading and trailing white space before it is
saved. For example,
<c:set var="name1" scope="page">
line 1
line 2
</c:set>
would be saved as:
"line 1\n line 2"
Related Examples
e1063.
Using the Java Standard Tag Library (JSTL) in a JSP Page
e1064.
Enabling the JSTL Expression Language in a JSP Page
e1065.
Getting a Request Parameter Using JSTL in a JSP Page
e1067.
Saving and Emitting HTML Fragments Using JSTL in a JSP Page
e1068.
Conditionally Generating Output Using JSTL in a JSP Page
© 2002 Addison-Wesley.