⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jspintro7.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">  <head>    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />    <meta http-equiv="Content-Style-Type" content="text/css" />    <title>Expression Language</title>    <link rel="StyleSheet" href="document.css" type="text/css" media="all" />    <link rel="StyleSheet" href="catalog.css" type="text/css" media="all" />    <link rel="Table of Contents" href="J2EETutorialTOC.html" />    <link rel="Previous" href="JSPIntro6.html" />    <link rel="Next" href="JSPIntro8.html" />    <link rel="Index" href="J2EETutorialIX.html" />  </head>  <body>    <table width="550" summary="layout" id="SummaryNotReq1">      <tr>	<td align="left" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/download.html#tutorial" target="_blank">Download</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/faq.html" target="_blank">FAQ</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/history.html" target="_blank">History</a>	</td>        <td align="center" valign="center"><a accesskey="p" href="JSPIntro6.html"><img id="LongDescNotReq1" src="images/PrevArrow.gif" width="26" height="26" border="0" alt="Prev" /></a><a accesskey="c" href="J2EETutorialFront.html"><img id="LongDescNotReq1" src="images/UpArrow.gif" width="26" height="26" border="0" alt="Home" /></a><a accesskey="n" href="JSPIntro8.html"><img id="LongDescNotReq3" src="images/NextArrow.gif" width="26" height="26" border="0" alt="Next" /></a><a accesskey="i" href="J2EETutorialIX.html"></a>        </td>	<td align="right" valign="center">	<font size="-1">	<a href="http://java.sun.com/j2ee/1.4/docs/api/index.html" target="_blank">API</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/search.html" target="_blank">Search</a>	<br>	<a href="http://java.sun.com/j2ee/1.4/docs/tutorial/information/sendusmail.html" target="_blank">Feedback</a></font>	</font>	</td>      </tr>    </table>    <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider">    <blockquote><a name="wp71019"> </a><h2 class="pHeading1">Expression Language</h2><a name="wp71020"> </a><p class="pBody">A primary feature of JSP technology version 2.0 is its support for an expression language. An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using a simple syntax such as</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">${name}<a name="wp71021"> </a></pre></div><a name="wp71022"> </a><p class="pBody">for a simple variable or </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">${name.foo.bar}<a name="wp71023"> </a></pre></div><a name="wp71024"> </a><p class="pBody">for a nested property.</p><a name="wp71025"> </a><p class="pBody">The <code class="cCode">test</code> attribute of the following conditional tag is supplied with an EL expression that compares the number of items in the session-scoped bean named <code class="cCode">cart</code> with 0:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:if test=&quot;${sessionScope.cart.numberOfItems &gt; 0}&quot;&gt; &nbsp;&nbsp;...&lt;/c:if&gt;<a name="wp71026"> </a></pre></div><a name="wp71029"> </a><p class="pBody">The JSP expression evaluator is responsible for handling EL expressions, which may include literals and are enclosed by the <code class="cCode">${ }</code> characters. For example: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:if test=&quot;${bean1.a &lt; 3}&quot; &gt;&nbsp;&nbsp;...&lt;/c:if&gt;<a name="wp71030"> </a></pre></div><a name="wp71031"> </a><p class="pBody">Any value that does not begin with <code class="cCode">${</code> is treated as a literal that is parsed to the expected type using the <code class="cCode">PropertyEditor</code> for the type: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:if test=&quot;true&quot; &gt;...&lt;/c:if&gt;<a name="wp71032"> </a></pre></div><a name="wp71033"> </a><p class="pBody">Literal values that contain the <code class="cCode">${</code> characters must be escaped as follows: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;mytags:example attr1=&quot;an expression is ${&#39;${&#39;}true}&quot; /&gt;<a name="wp71266"> </a></pre></div><a name="wp72527"> </a><h3 class="pHeading2">Deactivating Expression Evaluation</h3><a name="wp83256"> </a><p class="pBody">Since the pattern that identifies EL expressions--<code class="cCode">${ }</code>--was not reserved in the JSP specifications before JSP 2.0, there may be applications where such a pattern is intended to pass through verbatim. To prevent the pattern from being evaluated, EL evaluation can be deactivated.</p><a name="wp72528"> </a><p class="pBody">To deactivate the evaluation of EL expressions you specify the <code class="cCode">isELIgnored </code>attribute of the <code class="cCode">page</code> directive</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ page isELIgnored =&quot;true|false&quot; %&gt;<a name="wp72532"> </a></pre></div><a name="wp77548"> </a><p class="pBody">The valid values of this attribute are <code class="cCode">true</code> and <code class="cCode">false</code>. If <code class="cCode">true</code>, EL expressions are ignored when they appear in template text or tag attributes. If <code class="cCode">false</code>, EL expressions are evaluated by the container. </p><a name="wp83259"> </a><p class="pBody">The default value varies depending on the version of the Web application deployment descriptor. The default mode for JSP pages delivered using a Servlet 2.3 or earlier descriptor is to ignore EL expressions; this provides backwards compatibility. The default mode for JSP pages delivered with a Servlet 2.4 descriptor is to evaluate EL expressions; this automatically provides the default that most applications want. You can also deactivate EL expression evaluation for a group of JSP pages (see <a  href="JSPIntro13.html#wp83214">Deactivating EL Evaluation</a><code class="cCode">)</code>.</p><a name="wp71322"> </a><h3 class="pHeading2">Using Expressions</h3><a name="wp71323"> </a><p class="pBody">EL Expressions can be used in two situations:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp71543"> </a><div class="pSmartList1"><li>In template text</li></div><a name="wp71481"> </a><div class="pSmartList1"><li>In any standard or custom tag attribute that can accept an expression</li></div></ul></div><a name="wp71549"> </a><p class="pBody">The value of an expression in template text is computed and inserted into the current output. An expression <em class="cEmphasis">will not </em>be evaluated if the body of the tag is declared to be <code class="cCode">tagdependent</code> (see <a  href="JSPTags5.html#wp89848">body-content Attribute</a>). </p><a name="wp71324"> </a><p class="pBody">Three ways to set a tag attribute value: </p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp71325"> </a><div class="pSmartList1"><li>With a single expression construct:</li></div><a name="wp71326"> </a><p class="pBodyRelative"><code class="cCode">&lt;some:tag value=&quot;${expr}&quot;/&gt;</code></p><a name="wp71327"> </a><p class="pBodyRelative">The expression is evaluated and the result is coerced to the attribute's expected type. </p><a name="wp71328"> </a><div class="pSmartList1"><li>With one or more expressions separated or surrounded by text: </li></div><a name="wp71329"> </a><p class="pBodyRelative"><code class="cCode">&lt;some:tag value=&quot;some${expr}${expr}text${expr}&quot;/&gt;</code></p><a name="wp71330"> </a><p class="pBodyRelative">The expressions are evaluated from left to right. Each expression is coerced to a <code class="cCode">String</code> and then concatenated with any intervening text. The resulting <code class="cCode">String</code> is then coerced to the attribute's expected type. </p><a name="wp71331"> </a><div class="pSmartList1"><li>With only text: </li></div><a name="wp71332"> </a><p class="pBodyRelative"><code class="cCode">&lt;some:tag value=&quot;sometext&quot;/&gt;</code></p><a name="wp84837"> </a><p class="pBodyRelative">In this case, the attribute's <code class="cCode">String</code> value is coerced to the attribute&#39;s expected type.</p></ul></div><a name="wp84855"> </a><p class="pBody">Expressions used to set attribute values are evaluated in the context of an expected type. If the result of the expression evaluation does not match the expected type exactly a type conversion will be performed. For example, the expression <code class="cCode">${1.2E4 + 1.4}</code> provided as the value of an attribute of type <code class="cCode">float</code>, will result in the following conversion: <code class="cCode">Float.valueOf(&quot;1.2E4 + 1.4&quot;).floatValue()</code>. See Section JSP2.8 of the <a  href="http://java.sun.com/products/jsp/download.html#specs" target="_blank">JSP 2.0 Specification</a> for the complete type conversion rules. </p><a name="wp84857"> </a><h3 class="pHeading2">Variables</h3><a name="wp84858"> </a><p class="pBody">The JSP container evaluates a variable that appears in an expression by looking up its value according to the behavior of <code class="cCode">PageContext.findAttribute(String)</code>. For example, when evaluating the expression <code class="cCode">${product}</code>, the container will look for <code class="cCode">product</code> in the page, request, session, and application scopes and will return its value. If <code class="cCode">product</code> is not found, <code class="cCode">null</code> is returned. A variable that matches one of the implicit objects described in <a  href="JSPIntro7.html#wp71043">Implicit Objects</a> will return that implicit object instead of the variable's value.</p><a name="wp71646"> </a><p class="pBody">Properties of variables are accessed using the <code class="cCode">.</code> operator, and may be nested arbitrarily. </p><a name="wp76697"> </a><p class="pBody">The JSP expression language unifies the treatment of the <code class="cCode">.</code> and <code class="cCode">[]</code> operators. <code class="cCode">expr-a.expr-b</code> is equivalent to <code class="cCode">a[&quot;expr-b&quot;]</code>; that is, the expression <code class="cCode">expr-b</code> is used to construct a literal whose value is the identifier, and then the <code class="cCode">[]</code> operator is used with that value.</p><a name="wp76693"> </a><p class="pBody">To evaluate <code class="cCode">expr-a[expr-b]</code>, evaluate <code class="cCode">expr-a</code> into <code class="cCode">value-a</code> and evaluate <code class="cCode">expr-b</code> into <code class="cCode">value-b</code>. If either <code class="cCode">value-a</code> or <code class="cCode">value-b</code> is null, return null.</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp71038"> </a><div class="pSmartList1"><li>If <code class="cCode">value-a</code> is a <code class="cCode">Map</code>, return <code class="cCode">value-a.get(value-b)</code>. If <code class="cCode">!value-a.containsKey(value-b)</code>, then return <code class="cCode">null</code>.</li></div><a name="wp71039"> </a><div class="pSmartList1"><li>If <code class="cCode">value-a</code> is a <code class="cCode">List</code> or array, coerce <code class="cCode">value-b</code> to <code class="cCode">int</code> and return <code class="cCode">value-a.get(value-b)</code> or <code class="cCode">Array.get(value-a, value-b)</code>, as appropriate. If the coercion couldn't be performed, an error is returned. If the <code class="cCode">get</code> call returns an <code class="cCode">IndexOutOfBoundsException</code>, <code class="cCode">null</code> is returned. If the <code class="cCode">get</code> call returns another exception, an error is returned.</li></div><a name="wp71040"> </a><div class="pSmartList1"><li>If <code class="cCode">value-a</code> is a JavaBeans object, coerce <code class="cCode">value-b</code> to <code class="cCode">String</code>. If <code class="cCode">value-b</code> is a readable property of <code class="cCode">value-a</code>, then return the result of a <code class="cCode">get</code> call. If the <code class="cCode">get</code> method throws an exception, an error is returned.</li></div></ul></div><a name="wp71043"> </a><h3 class="pHeading2">Implicit Objects</h3><a name="wp71044"> </a><p class="pBody">The JSP expression language defines a set of implicit objects:

⌨️ 快捷键说明

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