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

📄 jstl5.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
  <tr align="left">    <td><a name="wp76825"> </a><div class="pCellBody"><code class="cCode">$sessionScope:profile</code></div></td>    <td><a name="wp76827"> </a><div class="pCellBody">The session-scoped EL variable named <code class="cCode">profile</code></div></td></tr>  <tr align="left">    <td><a name="wp76829"> </a><div class="pCellBody"><code class="cCode">$initParam:mycom.productId</code></div></td>    <td><a name="wp76831"> </a><div class="pCellBody">The <code class="cCode">String</code> value of the<code class="cCode"> mycom.productId</code> context parameter</div></td></tr></table></div><p class="pBody"></p><a name="wp89716"> </a><p class="pBody">The XML tags are illustrated in another version (<code class="cCode">bookstore5</code>) of the Duke's Bookstore application. This version replaces the database with an XML representation of the bookstore database which is retrieved from another Web application. The directions for building and deploying this version of the application are in <a  href="JSPX2.html#wp116707">The Example JSP Document</a>. A sample <code class="cCode">bookstore5.war</code> is provided in <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/provided-wars/</code>. </p><a name="wp86905"> </a><h3 class="pHeading2">Core Tags</h3><a name="wp64219"> </a><p class="pBody">The core XML tags provide basic functionality to easily parse and access XML data.</p><a name="wp65344"> </a><p class="pBody">The <code class="cCode">parse</code> tag parses an XML document and saves the resulting object in the EL variable specified by attribute <code class="cCode">var</code>. In <code class="cCode">bookstore5</code>, the XML document is parsed and saved to a context attribute in <code class="cCode"><a  href="../examples/web/bookstore5/web/parseBooks.txt" target="_blank">parseBooks.jsp</a></code>, which is included by all JSP pages that need access to the document:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:if test=&quot;${applicationScope:booklist == null}&quot; &gt; &nbsp;&nbsp;&lt;c:import url=&quot;${initParam.booksURL}&quot; var=&quot;xml&quot; /&gt;&nbsp;&nbsp;&lt;x:parse doc=&quot;${xml}&quot; var=&quot;booklist&quot; scope=&quot;application&quot; /&gt;&lt;/c:if&gt; <a name="wp75555"> </a></pre></div><a name="wp75446"> </a><p class="pBody">The <code class="cCode">set</code> and <code class="cCode">out</code> tags parallel the behavior described in <a  href="JSTL4.html#wp63882">Variable Support Tags</a> and <a  href="JSTL4.html#wp85708">Miscellaneous Tags</a> for the XPath local expression language. The <code class="cCode">set</code> tag evaluates an XPath expression and sets the result into a JSP EL variable specified by attribute <code class="cCode">var</code>. The <code class="cCode">out</code> tag evaluates an XPath expression on the current context node and outputs the result of the evaluation to the current <code class="cCode">JspWriter</code> object. </p><a name="wp85240"> </a><p class="pBody">The JSP page <code class="cCode"><a  href="../examples/web/bookstore5/web/bookdetails.txt" target="_blank">bookdetails.jsp</a></code> selects a book element whose <code class="cCode">id</code> attribute matches the request parameter <code class="cCode">bookId</code> and sets the <code class="cCode">abook</code> attribute. The <code class="cCode">out</code> tag then selects the book's title element and outputs the result.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;x:set var=&quot;abook&quot;&nbsp;&nbsp;select=&quot;$applicationScope.booklist/&nbsp;&nbsp;&nbsp;&nbsp;books/book[@id=$param:bookId]&quot; /&gt;&nbsp;&nbsp;&lt;h2&gt;&lt;x:out select=&quot;$abook/title&quot;/&gt;&lt;/h2&gt;<a name="wp75686"> </a></pre></div><a name="wp75452"> </a><p class="pBody">As you have just seen, <code class="cCode">x:set</code> stores an internal XML representation of a <em class="cEmphasis">node</em> retrieved using an XPath expression; it doesn't convert the selected node into a <code class="cCode">String</code> and store it. Thus, <code class="cCode">x:set</code> is primarily useful for storing parts of documents for later retrieval. </p><a name="wp75816"> </a><p class="pBody">If you want to store a <code class="cCode">String</code>, you need to use <code class="cCode">x:out</code> within <code class="cCode">c:set</code>. The <code class="cCode">x:out</code> tag converts the node to a <code class="cCode">String</code>, and <code class="cCode">c:set</code> then stores the <code class="cCode">String</code> as an EL variable. For example, <code class="cCode"><a  href="../examples/web/bookstore5/web/bookdetails.txt" target="_blank">bookdetails.jsp</a></code> stores an EL variable containing a book price, which is later provided as the value of a <code class="cCode">fmt</code> tag, as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;c:set var=&quot;price&quot;&gt;&nbsp;&nbsp;&lt;x:out select=&quot;$abook/price&quot;/&gt;&lt;/c:set&gt;&lt;h4&gt;&lt;fmt:message key=&quot;ItemPrice&quot;/&gt;: &nbsp;&nbsp;&lt;fmt:formatNumber value=&quot;${price}&quot; type=&quot;currency&quot;/&gt;  <a name="wp75833"> </a></pre></div><a name="wp75834"> </a><p class="pBody">The other option, which is more direct but requires that the user have more knowledge of XPath, is to coerce the node to a <code class="cCode">String</code> manually using XPath's <code class="cCode">string</code> function.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;x:set var=&quot;price&quot; select=&quot;string($abook/price)&quot;/&gt;<a name="wp75391"> </a></pre></div><a name="wp63718"> </a><h3 class="pHeading2">Flow Control Tags</h3><a name="wp64573"> </a><p class="pBody">The XML flow control tags parallel the behavior described in <a  href="JSTL4.html#wp74001">Flow Control Tags</a> for the XPath expression language.</p><a name="wp75667"> </a><p class="pBody">The JSP page<code class="cCode"><a  href="../examples/web/bookstore5/web/bookcatalog.txt" target="_blank"> bookcatalog.jsp</a></code> uses the <code class="cCode">forEach</code> tag to display all the books contained in <code class="cCode">booklist</code> as follows:</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">&lt;x:forEach var=&quot;book&quot; &nbsp;&nbsp;select=&quot;$applicationScope:booklist/books/*&quot;&gt;&nbsp;&nbsp;&lt;tr&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:set var=&quot;bookId&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;x:out select=&quot;$book/@id&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:set&gt;=&nbsp;&nbsp;&nbsp;&nbsp;&lt;td bgcolor=&quot;#ffffaa&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:url var=&quot;url&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value=&quot;/bookdetails&quot; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:param name=&quot;bookId&quot; value=&quot;${bookId}&quot; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:param name=&quot;Clear&quot; value=&quot;0&quot; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:url&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;a href=&quot;${url}&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;strong&gt;&lt;x:out select=&quot;$book/title&quot;/&gt;&amp;nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/strong&gt;&lt;/a&gt;&lt;/td&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;td bgcolor=&quot;#ffffaa&quot; rowspan=2&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:set var=&quot;price&quot;&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;x:out select=&quot;$book/price&quot;/&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:set&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fmt:formatNumber value=&quot;${price}&quot; type=&quot;currency&quot;/&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;td bgcolor=&quot;#ffffaa&quot; rowspan=2&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;c:url var=&quot;url&quot; value=&quot;/catalog&quot; &gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;c:param name=&quot;Add&quot; value=&quot;${bookId}&quot; /&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/c:url&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;${url}&quot;&gt;&amp;nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;fmt:message key=&quot;CartAdd&quot;/&gt;&amp;nbsp;&lt;/a&gt;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/td&gt;&nbsp;&nbsp;&lt;/tr&gt; &nbsp;&nbsp;&lt;tr&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;td bgcolor=&quot;#ffffff&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&amp;nbsp;&amp;nbsp;&lt;fmt:message key=&quot;By&quot;/&gt; &lt;em&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;x:out select=&quot;$book/firstname&quot;/&gt;&amp;nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;x:out select=&quot;$book/surname&quot;/&gt;&lt;/em&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/x:forEach&gt;<a name="wp75661"> </a></pre></div><a name="wp75662"> </a><h3 class="pHeading2">Transformation Tags</h3><a name="wp64586"> </a><p class="pBody">The <code class="cCode">transform</code> tag applies a transformation, specified by a XSLT stylesheet set by the attribute <code class="cCode">xslt</code>, to an XML document, specified by the attribute <code class="cCode">doc</code>. If the <code class="cCode">doc</code> attribute is not specified, the input XML document is read from the tag's body content. </p><a name="wp64605"> </a><p class="pBody">The <code class="cCode">param</code> subtag can be used along with <code class="cCode">transform</code> to set transformation parameters. The attributes <code class="cCode">name</code> and <code class="cCode">value</code> are used to specify the parameter. The value attribute is optional. If it is not specified the value is retrieved from the tag's body.</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="JSTL4.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="JSTL6.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 + -