attr.html

来自「java类库详细讲解」· HTML 代码 · 共 188 行

HTML
188
字号
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<TITLE>Saving Data Using JSTL in a JSP Page
(Java Developers Almanac Example)
</TITLE>
<META CONTENT="Patrick Chan" NAME="AUTHOR">
<META CONTENT="Code Examples from The Java Developers Almanac 1.4" NAME="DESCRIPTION">
<META CONTENT="Addison-Wesley/Patrick Chan" NAME="OWNER">
<META CONTENT="3/20/02" NAME="revision">
<STYLE TYPE="text/css">
<!--     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}    -->
</STYLE>
</HEAD>
<BODY>
<TABLE CELLSPACING="0" CELLPADDING="0" BORDER="0">
<TR>
<TD rowspan="3"><A HREF="/?l=ex"><IMG BORDER="0" ALIGN="BOTTOM" HSPACE="10" SRC="/egs/almanac14a.jpg"></A></TD><TD VALIGN="top"><font face="Times" size="6"><b>The Java Developers Almanac 1.4</b></font>
<br>
        Order this book from <a href="/cgi-bin/scripts/redirect.pl?l=ex&url=http://www.amazon.com/exec/obidos/ASIN/0201752808/xeo">Amazon</a>.
    </TD>
</TR>
<TR>
<TD align="right" valign="bottom">
<FORM method="get" action="/cgi-bin/search/find.pl">
<INPUT size="25" name="words" type="text"><INPUT value="Search" type="submit">
</FORM>
</TD>
</TR>
</TABLE>
<HR color="#6666cc">
<DIV ALIGN="LEFT">
<A HREF="/">Home</A>
    &gt;
    <A HREF="../index.html">List of Packages</A>
    &gt;
    <B><A HREF="../javax.servlet.jsp.jstl.core/pkg.html">javax.servlet.jsp.jstl.core</A></B><font color="#666666" SIZE="-2">
        &nbsp;[6 examples]
        </font>
</DIV><P>
  <h3>
    e1066.  
    Saving Data Using JSTL in a JSP Page</h3>

When a JSP page needs to save data for its processing, it must
specify a location, called the <font color="#0066ff"><i>scope</i></font>. See
<a href="../javax.servlet.jsp/attr.html" class="eglink"><font size="-1"><b>e1047</b> Saving Data in a JSP Page</font></a> for an explanation of the four
available scopes.

<P> Data is saved using a mechanism called scoped variables.  A
scoped variable has a name, which is of type <code>String</code> and a value,
which is of type <code>Object</code>.  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.

<P> When using the JSTL's expression language (see
<a href="../javax.servlet.jsp.jstl.core/uselang.html" class="eglink"><font size="-1"><b>e1064</b> Enabling the JSTL Expression Language in a JSP Page</font></a>), the variables in each
scope are made available in the implicit objects - <code>pageScope</code>,
<code>requestScope</code>, <code>sessionScope</code>, and <code>applicationScope</code>.

<P> This example saves and retrieves values in scoped variables in
each of the four scopes:


<pre>
    &lt;%-- Declare the core library --%&gt;
    &lt;%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %&gt;
    
    &lt;%-- Save data in scoped variables --%&gt;
    &lt;c:set var="name1" value="value1" scope="page" /&gt;
    &lt;c:set var="com_mycompany_name2" value="value2" scope="request" /&gt;
    &lt;c:set var="com_mycompany_name3" value="value3" scope="session" /&gt;
    &lt;c:set var="com_mycompany_name4" value="value4" scope="application" /&gt;
    
    &lt;%-- Show the saved values --%&gt;
    &lt;c:out value='${pageScope.name1}' /&gt;
    &lt;c:out value='${requestScope.com_mycompany_name2}' /&gt;
    &lt;c:out value='${sessionScope.com_mycompany_name3}' /&gt;
    &lt;c:out value='${applicationScope.com_mycompany_name4}' /&gt;
</pre>

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 - <code>pageScope</code>,
<code>requestScope</code>, <code>sessionScope</code>, and <code>applicationScope</code>.


<pre>
    &lt;%-- Show the saved values without a specific scope --%&gt;
    &lt;c:out value='${name1}' /&gt;
    &lt;c:out value='${com_mycompany_name2}' /&gt;
    &lt;c:out value='${com_mycompany_name3}' /&gt;
    &lt;c:out value='${com_mycompany_name4}' /&gt;
</pre>

It is also possible to specify the value to save using the contents
of the body, rather than through the <code>value</code> attribute:


<pre>
    &lt;%-- Save data using body content --%&gt;
    &lt;c:set var="name1" scope="page"&gt;
        value 1 in body
    &lt;/c:set&gt;
    &lt;c:set var="com_mycompany_name2" scope="request" &gt;
        value 2 in body
    &lt;/c:set&gt;
    &lt;c:set var="com_mycompany_name3" scope="session" &gt;
        value 3 in body
    &lt;/c:set&gt;
    &lt;c:set var="com_mycompany_name4" scope="application"&gt;
        value 4 in body
    &lt;/c:set&gt;
</pre>

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, 


<pre>
    &lt;c:set var="name1" scope="page"&gt;
        line 1
        line 2
    &lt;/c:set&gt;
</pre>
would be saved as:

<pre>
    "line 1\n    line 2"
</pre>
<P><table width="600" CELLSPACING="0" CELLPADDING="2" BORDER="0">
<tr>
<td bgcolor="#6666cc" align="center"><font color="#ffffff">
            &nbsp;Related Examples
        </font></td>
</tr>
</table>


e1063. <a class="eglink" href="usejstl.html?l=rel">
    Using the Java Standard Tag Library (JSTL) in a JSP Page
</a>
<br>

e1064. <a class="eglink" href="uselang.html?l=rel">
    Enabling the JSTL Expression Language in a JSP Page
</a>
<br>

e1065. <a class="eglink" href="getparam.html?l=rel">
    Getting a Request Parameter Using JSTL in a JSP Page
</a>
<br>

e1067. <a class="eglink" href="htmlfrag.html?l=rel">
    Saving and Emitting HTML Fragments Using JSTL in a JSP Page
</a>
<br>

e1068. <a class="eglink" href="if.html?l=rel">
    Conditionally Generating Output Using JSTL in a JSP Page
</a>
<br>


<br>

<br>
<FONT FACE="Verdana, Arial, Helvetica, sans-serif" SIZE="0">
&copy; 2002 Addison-Wesley.
</FONT>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?