📄 jstl7.html
字号:
value="${bookRow[7]}" /> <c:if test="${item.quantity > inventory.quantity}"> <c:set var="sufficientInventory" value="false" /> <h3><font color="red" size="+2"> <fmt:message key="OrderError"/> There is insufficient inventory for <i>${bookRow[3]}</i>.</font></h3> </c:if> </c:forEach> </c:forEach> <c:if test="${sufficientInventory == 'true'}" /> <c:forEach var="item" items="${sessionScope.cart.items}"> <c:set var="book" value="${item.item}" /> <c:set var="bookId" value="${book.bookId}" /> <sql:query var="books" sql="select * from PUBLIC.books where id = ?" > <sql:param value="${bookId}" /> </sql:query> <c:forEach var="bookRow" begin="0" items="${books.rows}"> <sql:update var="books" sql="update PUBLIC.books set inventory = inventory - ? where id = ?" > <sql:param value="${item.quantity}" /> <sql:param value="${bookId}" /> </sql:update> </c:forEach> </c:forEach> <h3><fmt:message key="ThankYou"/> ${param.cardname}.</h3><br> </c:if></sql:transaction><a name="wp84910"> </a></pre></div><a name="wp77082"> </a><h3 class="pHeading2">query Tag Result Interface</h3><a name="wp75351"> </a><p class="pBody">The <code class="cCode">Result</code> interface is used to retrieve information from objects returned from a <code class="cCode">query</code> tag. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative">public interface Result public String[] getColumnNames(); public int getRowCount() public Map[] getRows(); public Object[][] getRowsByIndex(); public boolean isLimitedByMaxRows();<a name="wp76211"> </a></pre></div><a name="wp75325"> </a><p class="pBody">For complete information about this interface, see the API documentation for the <code class="cCode"><a href="http://java.sun.com/j2ee/1.4/docs/api/index.htmlapi/javax/servlet/jsp/jstl/sql/package-summary.html" target="_blank">javax.servlet.jsp.jstl.sql</a></code> package.</p><a name="wp75363"> </a><p class="pBody">The <code class="cCode">var</code> attribute set by a <code class="cCode">query</code> tag is of type <code class="cCode">Result</code>. The <code class="cCode">getRows</code> method returns an array of maps that can be supplied to the <code class="cCode">items</code> attribute of a <code class="cCode">forEach</code> tag. The JSTL expression language converts the syntax <code class="cCode">${</code><code class="cVariable">result</code><code class="cCode">.rows}</code> to a call to <code class="cVariable">result</code><code class="cCode">.getRows</code>. The expression <code class="cCode">${books.rows}</code> in the following example returns an array of maps.</p><a name="wp70265"> </a><p class="pBody">When you provide a array of maps to the <code class="cCode">forEach</code> tag, the <code class="cCode">var</code> attribute set by the tag is of type <code class="cCode">Map</code>. To retrieve information from a row, use the <code class="cCode">get("</code><code class="cVariable">colname</code><code class="cCode">")</code> method to get a column value. The JSTL expression language converts the syntax <code class="cCode">${</code><code class="cVariable">map</code><code class="cCode">.</code><code class="cVariable">colname</code><code class="cCode">}</code> to a call to <code class="cVariable">map</code><code class="cCode">.get("</code><code class="cVariable">colname</code><code class="cCode">")</code>. For example, the expression <code class="cCode">${book.title}</code> returns the value of the title entry of a book map.</p><a name="wp70191"> </a><p class="pBody">The Duke's Bookstore page <code class="cCode"><a href="../examples/web/bookstore4/web/bookdetails.txt" target="_blank">bookdetails.jsp</a></code> retrieves the column values from the <code class="cCode">book</code> map as follows.</p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><c:forEach var="book" begin="0" items="${books.rows}"> <h2>${book.title}</h2> &nbsp;<fmt:message key="By"/> <em>${book.firstname} ${book.surname}</em>&nbsp;&nbsp; (${book.year})<br> &nbsp; <br> <h4><fmt:message key="Critics"/></h4> <blockquote>${book.description}</blockquote> <h4><fmt:message key="ItemPrice"/>: <fmt:formatNumber value="${book.price}" type="currency"/> </h4></c:forEach><a name="wp84884"> </a></pre></div><a name="wp68160"> </a><p class="pBody">The following excerpt from <code class="cCode"><a href="../examples/web/bookstore4/web/bookcatalog.txt" target="_blank">bookcatalog.jsp</a></code> uses the <code class="cCode">Row</code> interface to retrieve values from the columns of a book row using scripting language expressions. First the book row that matches a request parameter (<code class="cCode">bid</code>) is retrieved from the database. Since the <code class="cCode">bid</code> and <code class="cCode">bookRow</code> objects are later used by tags that use scripting language expressions to set attribute values and a scriptlet that adds a book to the shopping cart, both objects are declared as scripting variables using the <code class="cCode">jsp:useBean</code> tag. The page creates a bean that describes the book and scripting language expressions are used to set the book properties from book row column values. Finally the book is added to the shopping cart.</p><a name="wp69951"> </a><p class="pBody">You might want to compare this version of <code class="cCode">bookcatalog.jsp</code> to the versions in <a href="JSPIntro.html#wp69778">JavaServer Pages Technology</a> and <a href="JSPTags.html#wp74644">Custom Tags in JSP Pages</a> that use a book database JavaBeans component. </p><div class="pPreformattedRelative"><pre class="pPreformattedRelative"><sql:query var="books" dataSource="${applicationScope.bookDS}"> select * from PUBLIC.books where id = ? <sql:param value="${bid}" /></sql:query><c:forEach var="bookRow" begin="0" items="${books.rowsByIndex}"> <jsp:useBean id="bid" type="java.lang.String" /> <jsp:useBean id="bookRow" type="java.lang.Object[]" /> <jsp:useBean id="addedBook" class="database.BookDetails" scope="page" /> <jsp:setProperty name="addedBook" property="bookId" value="${bookRow[0]}" /> <jsp:setProperty name="addedBook" property="surname" value="${bookRow[1]}" /> <jsp:setProperty name="addedBook" property="firstName" value="${bookRow[2]}" /> <jsp:setProperty name="addedBook" property="title" value="${bookRow[3]}" /> <jsp:setProperty name="addedBook" property="price" value="${bookRow[4])}" /> <jsp:setProperty name="addedBook" property="year" value="${bookRow[6]}" /> <jsp:setProperty name="addedBook" property="description" value="${bookRow[7]}" /> <jsp:setProperty name="addedBook" property="inventory" value="${bookRow[8]}" /> </jsp:useBean> <% cart.add(bid, addedBook); %> ...</c:forEach><a name="wp84759"> </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="JSTL6.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="JSTL8.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 + -