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

📄 jsptags5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 4 页
字号:
</p><a name="wp90035"> </a><p class="pBody">In this example, the <code class="cCode">AT_END</code> scope is used to return a value to the page. The body of the tag is not affected.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%-- callingpage.jsp --%&gt;&lt;c:set var=&quot;x&quot; value=&quot;1&quot;/&gt;${x} &lt;%-- (<code class="cCodeBold">x == 1</code>) --%&gt;&lt;my:example&gt;&nbsp;&nbsp;${x} &lt;%-- (<code class="cCodeBold">x == 1</code>) --%&gt;&lt;/my:example&gt;${x} &lt;%-- (<code class="cCodeBold">x == 4</code>) --%&gt;<a name="wp90036"> </a>&lt;%-- example.tag --%&gt;&lt;%@ variable name-given=&quot;x&quot; scope=&quot;AT_END&quot; %&gt;${x} &lt;%-- (x == null) --%&gt;&lt;c:set var=&quot;x&quot; value=&quot;2&quot;/&gt;&lt;jsp:doBody/&gt;${x} &lt;%-- (x == 2) --%&gt;&lt;c:set var=&quot;x&quot; value=&quot;4&quot;/&gt;<a name="wp90037"> </a></pre></div><a name="wp90038"> </a><p class="pDefinitionTerm">AT_BEGIN and name-from-attribute</p><a name="wp90039"> </a><p class="pBody">In this example the <code class="cCode">AT_BEGIN</code> scope is used to pass an EL variable to the tag's body, and make it available to the calling page at the end of the tag invocation. The name of the variable is specified via the value of the attribute <code class="cCode">var</code>. The variable is referenced by a local name, <code class="cCode">result</code>, in the tag file.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%-- callingpage.jsp --%&gt;&lt;c:set var=&quot;x&quot; value=&quot;1&quot;/&gt;${x} &lt;%-- (x == 1) --%&gt;&lt;my:example var=&quot;x&quot;&gt;&nbsp;&nbsp;${x} &lt;%-- (x == 2) --%&gt;&nbsp;&nbsp;${result} &lt;%-- (result == null) --%&gt;&nbsp;&nbsp;&lt;c:set var=&quot;result&quot; value=&quot;invisible&quot;/&gt;&lt;/my:example&gt;${x} &lt;%-- (x == 4) --%&gt;${result} &lt;%-- (result == `invisible') --%&gt;<a name="wp90040"> </a>&lt;%-- example.tag --%&gt;&lt;%@ attribute name=&quot;var&quot; required=&quot;true&quot; rtexprvalue=&quot;false&quot;%&gt;&lt;%@ variable alias=&quot;result&quot; name-from-attribute=&quot;var&quot;&nbsp;&nbsp;scope=&quot;AT_BEGIN&quot; %&gt;${x} &lt;%-- (x == null) --%&gt;${result} &lt;%-- (result == null) --%&gt;&lt;c:set var=&quot;x&quot; value=&quot;ignored&quot;/&gt;&lt;c:set var=&quot;result&quot; value=&quot;2&quot;/&gt;&lt;jsp:doBody/&gt;${x} &lt;%-- (x == `ignored') --%&gt;${result} &lt;%-- (result == 2) --%&gt;&lt;c:set var=&quot;result&quot; value=&quot;4&quot;/&gt;<a name="wp90041"> </a></pre></div><a name="wp90043"> </a><h3 class="pHeading2">Evaluating Fragments Passed to Tag Files</h3><a name="wp90044"> </a><p class="pBody">When a tag file is executed, the Web container passes it two types of fragments: fragment attributes and the tag body, which is implemented as a fragment. Recall from the discussion of fragment attributes that fragments are evaluated by the tag handler as opposed to the Web container. Within a tag file, you use the <code class="cCode">jsp:invoke</code> element to evaluate a fragment attribute and the <code class="cCode">jsp:doBody</code> element to evaluate a tag file body.</p><a name="wp90045"> </a><p class="pBody">The result of evaluating either type of fragment is sent to the response or stored in an EL variable for later manipulation. To store the result of evaluating a fragment to an EL variable, you specify the <code class="cCode">var</code> or <code class="cCode">varReader</code> attributes. If <code class="cCode">var</code> is specified, the container stores the result in an EL variable of type <code class="cCode">String</code> with the name specified by <code class="cCode">var</code>. If <code class="cCode">varReader</code> is specified, the container stores the result in an EL variable of type <code class="cCode">java.io.Reader</code> with the name specified by <code class="cCode">varReader</code>. The <code class="cCode">Reader</code> object can then be passed to a custom tag for further processing. A translation error occurs if both <code class="cCode">var</code> and <code class="cCode">varReader</code> are specified. </p><a name="wp90046"> </a><p class="pBody">An optional <code class="cCode">scope</code> attribute indicates the scope of the resulting variable. The possible values are <code class="cCode">page</code> (default), <code class="cCode">request</code>, <code class="cCode">session</code>, or <code class="cCode">application</code>. A translation error occurs if this attribute appears without specifying the <code class="cCode">var</code> or <code class="cCode">varReader</code> attribute. </p><a name="wp90047"> </a><h3 class="pHeading2">Examples</h3><a name="wp90048"> </a><h4 class="pHeading3">Simple Attributes</h4><a name="wp90050"> </a><p class="pBody">The Duke's Bookstore <code class="cCode">shipDate</code> tag, defined in <code class="cCode"><a  href="../examples/web/bookstore3/web/shipDate.txt" target="_blank">shipDate.tag</a></code>, is a custom tag with a simple attribute. The tag generates the date of a book order according to the type of shipping requested. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ taglib prefix=&quot;sc&quot; tagdir=&quot;/WEB-INF/tags&quot; %&gt;&lt;h3&gt;&lt;fmt:message key=&quot;ThankYou&quot;/&gt; ${param.cardname}.&lt;/h3&gt;&lt;br&gt;&lt;fmt:message key=&quot;With&quot;/&gt; &lt;em&gt;&lt;fmt:message key=&quot;${param.shipping}&quot;/&gt;&lt;/em&gt;,  &lt;fmt:message key=&quot;ShipDateLC&quot;/&gt;&lt;sc:shipDate shipping=&quot;${param.shipping}&quot; /&gt; <a name="wp90051"> </a></pre></div><a name="wp90052"> </a><p class="pBody">The tag determines the number of days until shipment from the <code class="cCode">shipping</code> attribute passed to it by the page <code class="cCode"><a  href="../examples/web/bookstore3/web/bookreceipt.txt" target="_blank">bookreceipt.jsp</a></code>. From the days, the tag computes the ship date. It then formats the ship date.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ attribute name=&quot;shipping&quot; required=&quot;true&quot; %&gt;&lt;jsp:useBean id=&quot;now&quot; class=&quot;java.util.Date&quot; /&gt;&lt;jsp:useBean id=&quot;shipDate&quot; class=&quot;java.util.Date&quot; /&gt;&lt;c:choose&gt;  &lt;c:when test=&quot;${shipping == &#39;QuickShip&#39;}&quot;&gt;    &lt;c:set var=&quot;days&quot; value=&quot;2&quot; /&gt;  &lt;/c:when&gt;  &lt;c:when test=&quot;${shipping == &#39;NormalShip&#39;}&quot;&gt;    &lt;c:set var=&quot;days&quot; value=&quot;5&quot; /&gt;  &lt;/c:when&gt;  &lt;c:when test=&quot;${shipping == &#39;SaverShip&#39;}&quot;&gt;     &lt;c:set var=&quot;days&quot; value=&quot;7&quot; /&gt;  &lt;/c:when&gt;&lt;/c:choose&gt;&lt;jsp:setProperty name=&quot;shipDate&quot; property=&quot;time&quot;&nbsp;&nbsp;value=&quot;${now.time + 86400000 * days}&quot; /&gt;&lt;fmt:formatDate value=&quot;${shipDate}&quot; type=&quot;date&quot;&nbsp;&nbsp;dateStyle=&quot;full&quot;/&gt;.&lt;br&gt;&lt;br&gt;<a name="wp90054"> </a></pre></div><a name="wp90055"> </a><h4 class="pHeading3">Simple and Fragment Attributes and Variables</h4><a name="wp90057"> </a><p class="pBody">The Duke's Bookstore <code class="cCode">catalog</code> tag, defined in <code class="cCode"><a  href="../examples/web/bookstore3/web/catalog.txt" target="_blank">catalog.tag</a></code>, is a custom tag with simple and fragment attributes and variables. The tag renders the catalog of a book database as an HTML table. The tag file declares that it sets variables named <code class="cCode">price</code> and <code class="cCode">salePrice</code> via <code class="cCode">variable</code> directives. The fragment <code class="cCode">normalPrice</code> uses the variable <code class="cCode">price</code> and the fragment <code class="cCode">onSale</code> uses the variables <code class="cCode">price</code> and <code class="cCode">salePrice</code>. Before the tag invokes the fragment attributes with the <code class="cCode">jsp:invoke</code> element, the Web container passes values for the variables back to the calling page.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ attribute name=&quot;bookDB&quot; required=&quot;true&quot;&nbsp;&nbsp;type=&quot;database.BookDB&quot; %&gt;&lt;%@ attribute name=&quot;color&quot; required=&quot;true&quot; %&gt;&lt;%@ attribute name=&quot;normalPrice&quot; fragment=&quot;true&quot; %&gt;&lt;%@ attribute name=&quot;onSale&quot; fragment=&quot;true&quot; %&gt;&lt;%@ variable name-given=&quot;price&quot; %&gt; &lt;%@ variable name-given=&quot;salePrice&quot; %&gt;&lt;center&gt;&lt;table&gt;&lt;c:forEach var=&quot;book&quot; begin=&quot;0&quot; items=&quot;${bookDB.books}&quot;&gt;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp;&lt;c:set var=&quot;bookId&quot; value=&quot;${book.bookId}&quot; /&gt;&nbsp;&nbsp;&lt;td bgcolor=&quot;${color}&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;c:url var=&quot;url&quot; value=&quot;/bookdetails&quot; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:param name=&quot;bookId&quot; value=&quot;${bookId}&quot; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:url&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&quot;${url}&quot;&gt;&lt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strong&gt;${book.title}&amp;nbsp;&lt;/strong&gt;&lt;/a&gt;&lt;/td&gt; &nbsp;&nbsp;&lt;td bgcolor=&quot;${color}&quot; rowspan=2&gt;&nbsp;&nbsp;&lt;c:set var=&quot;salePrice&quot; value=&quot;${book.price * .85}&quot; /&gt;&nbsp;&nbsp;&lt;c:set var=&quot;price&quot; value=&quot;${book.price}&quot; /&gt;&nbsp;&nbsp;&lt;c:choose&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:when test=&quot;${book.onSale}&quot; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;jsp:invoke fragment=&quot;onSale&quot; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:when&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:otherwise&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;jsp:invoke fragment=&quot;normalPrice&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:otherwise&gt;&nbsp;&nbsp;&lt;/c:choose&gt;&nbsp;&nbsp;&amp;nbsp;&lt;/td&gt; ...&lt;/table&gt;&lt;/center&gt;<a name="wp90058"> </a></pre></div><a name="wp90060"> </a><p class="pBody">The page <code class="cCode"><a  href="../examples/web/bookstore3/web/bookcatalog.txt" target="_blank">bookcatalog.jsp</a></code> invokes the <code class="cCode">catalog</code> tag with simple attributes <code class="cCode">bookDB</code>, which contains catalog data, and <code class="cCode">color</code>, which customizes the coloring of the table rows. The formatting of the book price is determined by two fragment attributes--<code class="cCode">normalPrice</code> and <code class="cCode">onSale</code>--that are conditionally invoked by the tag according to data retrieved from the book database.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;sc:catalog bookDB =&quot;${bookDB}&quot; color=&quot;#cccccc&quot;&gt;&nbsp;&nbsp;&lt;jsp:attribute name=&quot;normalPrice&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fmt:formatNumber value=&quot;${price}&quot; type=&quot;currency&quot;/&gt;&nbsp;&nbsp;&lt;/jsp:attribute&gt;&nbsp;&nbsp;&lt;jsp:attribute name=&quot;onSale&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;strike&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fmt:formatNumber value=&quot;${price}&quot; type=&quot;currency&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/strike&gt;&lt;br/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;font color=&quot;red&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fmt:formatNumber value=&quot;${salePrice}&quot; type=&quot;currency&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/font&gt;&nbsp;&nbsp;&lt;/jsp:attribute&gt;&lt;/sc:catalog&gt;<a name="wp90061"> </a></pre></div><a name="wp90065"> </a><p class="pBody">The screen produced by <code class="cCode">bookcatalog.jsp</code> is shown in <a  href="JSPTags5.html#wp90078">Figure 15-2</a>. You can compare it to the version in <a  href="JSPIntro3.html#wp78717">Figure 12-2</a>.</p><a name="wp90076"> </a><p class="pBody"></p><div align="left"><img src="images/bookcatalog22.gif" height="502" width="407" alt="Duke's Bookstore Book Catalog" border="0" hspace="0" vspace="0"/></div><p class="pBody"></p><p>  <a name="90078"> </a><strong><font >Figure 15-2    Book Catalog</font></strong></p><a name="wp90079"> </a><h4 class="pHeading3">Dynamic Attributes</h4><a name="wp90082"> </a><p class="pBody">The following code implements the tag discussed in <a  href="JSPTags4.html#wp89615">Dynamic Attributes</a>. An arbitrary number of attributes whose values are colors are stored in a <code class="cCode">Map</code> named by the <code class="cCode">dynamic-attributes</code> attribute of the <code class="cCode">tag</code> directive. The JSTL <code class="cCode">forEach</code> tag is used to iterate through the <code class="cCode">Map</code> and the attribute keys and colored attribute values are printed in a bulleted list.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;%@ tag dynamic-attributes=&quot;colorMap&quot;%&gt;&lt;ul&gt;&lt;c:forEach var=&quot;color&quot; begin=&quot;0&quot; items=&quot;${colorMap}&quot;&gt;&nbsp;&nbsp;&lt;li&gt;${color.key} = &nbsp;&nbsp;&nbsp;&nbsp;&lt;font color=&quot;${color.value}&quot;&gt;${color.value}&lt;/font&gt;&lt;li&gt;&lt;/c:forEach&gt;&lt;/ul&gt;<a name="wp90084"> </a></pre></div>    </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="JSPTags4.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="JSPTags6.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 + -