📄 jstl4.html
字号:
<% cart.add(bid, addedBook); %>...</c:if><a name="wp73413"> </a></pre></div><a name="wp73414"> </a><p class="pBody">The <code class="cCode">choose</code> tag performs conditional block execution by the embedded <code class="cCode">when</code> sub tags. It renders the body of the first <code class="cCode">when</code> tag whose test condition evaluates to true. If none of the test conditions of nested <code class="cCode">when</code> tags evaluate to <code class="cCode">true</code>, then the body of an <code class="cCode">otherwise</code> tag is evaluated, if present. </p><a name="wp81081"> </a><p class="pBody">For example, the following sample code shows how to render text based on a customer's membership category. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:choose> <c:when test="${customer.category == 'trial'}" > ... </c:when> <c:when test="${customer.category == 'member'}" > ... </c:when> <c:when test="${customer.category == 'preferred'}" > ... </c:when> <c:otherwise> ... </c:otherwise> </c:choose> <a name="wp81548"> </a></pre></div><a name="wp81533"> </a><p class="pBody">The <code class="cCode">choose</code>, <code class="cCode">when</code>, and <code class="cCode">otherwise</code> tags can be used to construct an <code class="cCode">if</code>-<code class="cCode">then</code>-<code class="cCode">else</code> statement as follows: </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:choose> <c:when test="${count == 0}" > No records matched your selection. </c:when> <c:otherwise> ${count} records matched your selection. </c:otherwise> </c:choose><a name="wp81544"> </a></pre></div><a name="wp63713"> </a><h4 class="pHeading3">Iterator Tags</h4><a name="wp66776"> </a><p class="pBody">The <code class="cCode">forEach</code> tag allows you to iterate over a collection of objects. You specify the collection via the <code class="cCode">items</code> attribute, and the current item is available through a scope variable named by the <code class="cCode">item</code> attribute.</p><a name="wp81610"> </a><p class="pBody">A large number of collection types are supported by <code class="cCode">forEach</code>, including all implementations of <code class="cCode">java.util.Collection</code> and <code class="cCode">java.util.Map</code>. If the <code class="cCode">items</code> attribute is of type <code class="cCode">java.util.Map</code>, then the current item will be of type <code class="cCode">java.util.Map.Entry</code>, which has the following properties:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp81582"> </a><div class="pSmartList1"><li><code class="cCode">key</code> - the key under which the item is stored in the underlying <code class="cCode">Map</code></li></div><a name="wp81583"> </a><div class="pSmartList1"><li><code class="cCode">value</code> - the value that corresponds to the key</li></div></ul></div><a name="wp81572"> </a><p class="pBody">Arrays of objects as well as arrays of primitive types (for example, <code class="cCode">int</code>) are also supported. For arrays of primitive types, the current item for the iteration is automatically wrapped with its standard wrapper class (for example, <code class="cCode">Integer</code> for <code class="cCode">int</code>, <code class="cCode">Float</code> for <code class="cCode">float</code>, and so on). </p><a name="wp81581"> </a><p class="pBody">Implementations of <code class="cCode">java.util.Iterator</code> and <code class="cCode">java.util.Enumeration</code> are supported but these must be used with caution. <code class="cCode">Iterator</code> and <code class="cCode">Enumeration</code> objects are not resettable so they should not be used within more than one iteration tag. Finally, <code class="cCode">java.lang.String</code> objects can be iterated over if the string contains a list of comma separated values (for example: Monday,Tuesday,Wednesday,Thursday,Friday).</p><a name="wp81619"> </a><p class="pBody">Here's the shopping cart iteration from the previous section with the <code class="cCode">forEach</code> tag:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:forEach var="item" items="${sessionScope.cart.items}"> ... <tr> <td align="right" bgcolor="#ffffff"> ${item.quantity} </td> ...</c:forEach><a name="wp81620"> </a></pre></div><a name="wp81567"> </a><p class="pBody">The <code class="cCode">forTokens</code> tag is used to iterate over a collection of tokens separated by a delimiter.</p><a name="wp64122"> </a><h3 class="pHeading2">URL Tags</h3><a name="wp64935"> </a><p class="pBody">The <code class="cCode">jsp:include</code> element provides for the inclusion of static and dynamic resources in the same context as the current page. However, <code class="cCode">jsp:include</code> cannot access resources that reside outside of the Web application and causes unnecessary buffering when the resource included is used by another element.</p><a name="wp64951"> </a><p class="pBody">In the example below, the <code class="cCode">transform</code> element uses the content of the included resource as the input of its transformation. The <code class="cCode">jsp:include</code> element reads the content of the response, writes it to the body content of the enclosing transform element, which then re-reads the exact same content. It would be more efficient if the <code class="cCode">transform</code> element could access the input source directly and avoid the buffering involved in the body content of the transform tag.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><acme:transform> <jsp:include page="/exec/employeesList"/><acme:transform/><a name="wp64956"> </a></pre></div><a name="wp69696"> </a><p class="pBody">The <code class="cCode">import</code> tag is therefore the simple, generic way to access URL-based resources whose content can then be included and or processed within the JSP page. For example, in <a href="JSTL5.html#wp63716">XML Tags</a>, <code class="cCode">import</code> is used to read in the XML document containing book information and assign the content to the scoped variable <code class="cCode">xml</code>:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:import url="/books.xml" var="xml" /><x:parse doc="${xml}" var="booklist" scope="application" /><a name="wp75969"> </a></pre></div><a name="wp75967"> </a><p class="pBody">The <code class="cCode">param</code> tag, analogous to the <code class="cCode">jsp:param</code> tag (see <a href="JSPIntro11.html#wp66264">jsp:param Element</a>), can be used with <code class="cCode">import</code> to specify request parameters.</p><a name="wp73424"> </a><p class="pBody">In <a href="Servlets11.html#wp64784">Session Tracking</a> we discussed how an application must rewrite URLs to enable session tracking whenever the client turns off cookies. You can use the <code class="cCode">url</code> tag to rewrite URLs returned from a JSP page. The tag includes the session ID in the URL only if cookies are disabled; otherwise, it returns the URL unchanged. Note that this feature requires the URL to be <em class="cEmphasis">relative</em>. The <code class="cCode">url</code> tag takes <code class="cCode">param</code> subtags for including parameters in the returned URL. For example, <code class="cCode"><a href="../examples/web/bookstore4/web/bookcatalog.txt" target="_blank">bookcatalog.jsp</a></code> rewrites the URL used to add a book to the shopping cart as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:url var="url" value="/catalog" > <c:param name="Add" value="${bookId}" /></c:url><p><strong><a href="${url}"><a name="wp74593"> </a></pre></div><a name="wp73452"> </a><p class="pBody"><code class="cCode">The redirect</code> tag sends an HTTP redirect to the client. The <code class="cCode">redirect</code> tag takes <code class="cCode">param</code> subtags for including parameters in the returned URL.</p><a name="wp85708"> </a><h3 class="pHeading2">Miscellaneous Tags</h3><a name="wp85666"> </a><p class="pBody">The <code class="cCode">catch</code> tag provides a complement to the JSP error page mechanism. It allows page authors to recover gracefully from error conditions that they can control. Actions that are of central importance to a page should <em class="cEmphasis">not</em> be encapsulated in a <code class="cCode">catch</code>, so their exceptions will propagate to an error page. Actions with secondary importance to the page should be wrapped in a <code class="cCode">catch</code>, so they never cause the error page mechanism to be invoked.</p><a name="wp85667"> </a><p class="pBody">The exception thrown is stored in the variable identified by <code class="cCode">var</code>, which always has page scope. If no exception occurred, the scoped variable identified by <code class="cCode">var</code> is removed if it existed. If <code class="cCode">var</code> is missing, the exception is simply caught and not saved.</p><a name="wp85668"> </a><p class="pBody">The <code class="cCode">out</code> tag evaluates an expression and outputs the result of the evaluation to the current <code class="cCode">JspWriter</code> object. The syntax and attributes are </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:out value="<code class="cVariable">value</code>" [escapeXml="{true|false}"] [default="<code class="cVariable">defaultValue</code>"] /><a name="wp85669"> </a></pre></div><a name="wp85670"> </a><p class="pBody">If the result of the evaluation is a <code class="cCode">java.io.Reader</code> object, data is first read from the <code class="cCode">Reader</code> object and then written into the current <code class="cCode">JspWriter</code> object. The special processing associated with <code class="cCode">Reader</code> objects improves performance when large amount of data must be read and then written to the response.</p><a name="wp85705"> </a><p class="pBody">If <code class="cCode">escapeXml</code> is true, the character conversions listed in <a href="JSTL4.html#wp85678">Table 14-4</a> are applied:</p><div align="left"><table border="1" summary="Character Conversions" id="wp85678"> <caption><a name="wp85678"> </a><div class="pTableTitle">Table 14-4 Character Conversions</div></caption> <tr align="center"> <th><a name="wp85682"> </a><div class="pCellHeading">Character</div></th> <th><a name="wp85684"> </a><div class="pCellHeading">Character Entity Code</div></th></tr> <tr align="left"> <td><a name="wp85686"> </a><div class="pCellBody"><code class="cCode"><</code></div></td> <td><a name="wp85688"> </a><div class="pCellBody"><code class="cCode">&lt;</code></div></td></tr> <tr align="left"> <td><a name="wp85690"> </a><div class="pCellBody"><code class="cCode">></code></div></td> <td><a name="wp85692"> </a><div class="pCellBody"><code class="cCode">&gt;</code></div></td></tr> <tr align="left"> <td><a name="wp85694"> </a><div class="pCellBody"><code class="cCode">&</code></div></td> <td><a name="wp85696"> </a><div class="pCellBody"><code class="cCode">&amp;</code></div></td></tr> <tr align="left"> <td><a name="wp85698"> </a><div class="pCellBody">'</div></td> <td><a name="wp85700"> </a><div class="pCellBody"><code class="cCode">&#039;</code></div></td></tr> <tr align="left"> <td><a name="wp85702"> </a><div class="pCellBody"><code class="cCode">"</code></div></td> <td><a name="wp85704"> </a><div class="pCellBody"><code class="cCode">&#034;</code></div></td></tr></table></div><p class="pBody"></p> </blockquote> <img src="images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" NATURALSIZEFLAG="3" ALT="Divider"> <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="JSTL3.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="JSTL5.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"><p><font size="-1">All of the material in <em>The J2EE(TM) 1.4 Tutorial</em> is <a href="J2EETutorialFront2.html">copyright</a>-protected and may not be published in other workswithout express written permission from Sun Microsystems.</font> </body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -