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

📄 jspintro3.html

📁 j2eePDF格式的电子书
💻 HTML
📖 第 1 页 / 共 2 页
字号:
&nbsp;&nbsp;public void setDatabase(BookDAO database) {&nbsp;&nbsp;&nbsp;&nbsp;this.database = database;&nbsp;&nbsp;}&nbsp;&nbsp;public BookDetails getBookDetails() &nbsp;&nbsp;&nbsp;&nbsp;throws Exception {&nbsp;&nbsp;&nbsp;&nbsp;return (BookDetails)database.getBookDetails(bookId);&nbsp;&nbsp;}&nbsp;&nbsp;...}<a name="wp65873"> </a></pre></div><a name="wp75556"> </a><p class="pBody">This version of the Duke's Bookstore application is organized along the Model-View-Controller (MVC) architecture. The MVC architecture is a widely-used architectural approach for interactive applications that separates functionality among application objects so as to minimize the degree of coupling between the objects. To achieve this, it divides applications into three layers: model, view, and controller. Each layer handles specific tasks and has responsibilities to the other layers:</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp75557"> </a><div class="pSmartList1"><li>The model represents business data and business logic or operations that govern access and modification of this business data. The model notifies views when it changes and provides the ability for the view to query the model about its state. It also provides the ability for the controller to access application functionality encapsulated by the model. In the Duke's Bookstore application, the shopping cart and database helper object contain the business logic for the application. </li></div><a name="wp75518"> </a><div class="pSmartList1"><li>The view renders the contents of a model. It gets data from the model and specifies how that data should be presented. It updates data presentation when the model changes. A view also forwards user input to a controller. The Duke's Bookstore JSP pages format the data stored in the session-scoped shopping cart and the page-scoped database helper object.</li></div><a name="wp75458"> </a><div class="pSmartList1"><li>The controller defines application behavior. It dispatches user requests and <a  href="JSPTags.html#wp74641">15</a>selects views for presentation. It interprets user inputs and maps them into actions to be performed by the model. In a Web application, user inputs are HTTP GET and POST requests. A controller selects the next view to display based on the user interactions and the outcome of the model operations. In the Duke's Bookstore application, the <code class="cCode">Dispatcher</code> servlet is the controller. It examines the request URL, creates and initializes a session-scoped JavaBeans component--the shopping cart--and dispatches requests to view JSP pages.</li></div></ul></div><hr><a name="wp77376"> </a><p class="pNote">Note: When employed in a Web application, the MVC architecture is often referred to as a Model-2 architecture. The bookstore example discussed in the previous chapter, which intermixes presentation and business logic, follows what is known as a Model-1 architecture. The Model-2 architecture is the recommended approach to designing Web applications.</p><hr><a name="wp75429"> </a><p class="pBody">In addition, this version of the application uses several custom tags from the JavaServer Pages Standard Tag Library (JSTL) (see Chapter&nbsp;<a  href="JSTL.html#wp74644"></a>):</p><div class="pSmartList1"><ul class="pSmartList1"><a name="wp75576"> </a><div class="pSmartList1"><li><code class="cCode">c:if</code> and <code class="cCode">c:choose</code>, <code class="cCode">c:when</code>, and <code class="cCode">c:otherwise</code> for flow control</li></div><a name="wp75637"> </a><div class="pSmartList1"><li><code class="cCode">c:set</code> for setting scoped variables</li></div><a name="wp75631"> </a><div class="pSmartList1"><li><code class="cCode">c:url</code> for encoding URLs</li></div><a name="wp75642"> </a><div class="pSmartList1"><li><code class="cCode">fmt:message</code>, <code class="cCode">fmt:formatNumber</code>, and <code class="cCode">fmt:formatDate</code> for providing locale-sensitive messages, numbers, and dates</li></div></ul></div><a name="wp81811"> </a><p class="pBody">Custom tags are the preferred mechanism for performing a wide variety of dynamic processing tasks, including accessing databases, using enterprise services such as e-mail and directories, and flow control. In earlier versions of JSP technology, such tasks were performed with JavaBeans components in conjunction with scripting elements (discussed in Chapter&nbsp;<a  href="JSPAdvanced.html#wp65706">16</a>). Though still available in JSP 2.0, scripting elements tend to make JSP pages more difficult to maintain because they mix presentation and logic, which is discouraged in page design. Custom tags are introduced in <a  href="JSPIntro9.html#wp73314">Using Custom Tags</a> and described in detail in Chapter <a  href="JSPTags.html#wp74641">15</a>.</p><a name="wp81821"> </a><p class="pBody">Finally, this version of the example contains an applet to generate a dynamic digital clock in the banner. See <a  href="JSPIntro12.html#wp66270">Including an Applet</a> for a description of the JSP element that generates HTML for downloading the applet.</p><div class="pSmartList1"><ol type="1" class="pSmartList1"><a name="wp85055"> </a><div class="pSmartList1"><li>The source code for the application is located in the <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/bookstore2/</code> directory (see <a  href="About.html#wp67678">Building and Running the Examples</a>). A sample <code class="cCode">bookstore2.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>. To build, package, deploy, and run the example:</li></div><a name="wp84507"> </a><div class="pSmartList1"><li>Build and package the bookstore common files as described in <a  href="WebApp12.html#wp189559">Duke's Bookstore Examples</a>. </li></div><a name="wp84494"> </a><div class="pSmartList1"><li>In a terminal window, go to <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/bookstore2/</code>.</li></div><a name="wp69480"> </a><div class="pSmartList1"><li>Run <code class="cCode">asant</code> <code class="cCode">build</code>. This target will spawn any necessary compilations and copy files to the <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/bookstore2/build/</code> directory. </li></div><a name="wp85537"> </a><div class="pSmartList1"><li>Start the J2EE application server.</li></div><a name="wp83961"> </a><div class="pSmartList1"><li>Perform all the operations described in <a  href="WebApp13.html#wp69868">Accessing Databases from Web Applications</a>.</li></div><a name="wp96950"> </a><div class="pSmartList1"><li>Start <code class="cCode">deploytool</code>. </li></div><a name="wp96951"> </a><div class="pSmartList1"><li>Create a Web application called <code class="cCode">bookstore2</code> by running the New Web Component Wizard. Select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>New<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Web Component.</li></div><a name="wp96952"> </a><div class="pSmartList1"><li>New Web Component Wizard</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp96953"> </a><div class="pSmartList2"><li>Select the Create New Stand-Alone WAR Module radio button.</li></div><a name="wp96954"> </a><div class="pSmartList2"><li>Click Browse.</li></div><a name="wp96955"> </a><div class="pSmartList2"><li>In the WAR Location field, enter <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/bookstore2/bookstore2.war</code>. </li></div><a name="wp96956"> </a><div class="pSmartList2"><li>In the WAR Name field, enter <code class="cCode">bookstore2</code>.</li></div><a name="wp96957"> </a><div class="pSmartList2"><li>In the Context Root field, enter <code class="cCode">/bookstore2</code>.</li></div><a name="wp96958"> </a><div class="pSmartList2"><li>Click Edit.</li></div><a name="wp96959"> </a><div class="pSmartList2"><li>In the Edit Contents dialog, navigate to <code class="cCode">&lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/web/bookstore2/build/</code>. Select the JSP pages <code class="cCode">bookstore.jsp</code>, <code class="cCode">bookdetails.jsp</code>, <code class="cCode">bookcatalog.jsp</code>, <code class="cCode">bookshowcart.jsp</code>, <code class="cCode">bookcashier.jsp</code>, <code class="cCode">bookordererror.jsp</code>, <code class="cCode">bookreceipt.jsp</code>, <code class="cCode">duke.books.gif</code>,<code class="cCode"> </code>and the <code class="cCode">clock</code>, <code class="cCode">dispatcher</code>, <code class="cCode">database</code>, <code class="cCode">listeners</code>, and <code class="cCode">template</code> directories and click Add. </li></div><a name="wp96960"> </a><div class="pSmartList2"><li>Move <code class="cCode">/WEB-INF/classes/clock/</code> to the root directory of the WAR. By default, <code class="cCode">deploytool</code> packages all classes in <code class="cCode">/WEB-INF/classes/</code>. Since <code class="cCode">clock/DigitalClock.class</code> is a client-side class, it must be packaged in the root directory. To do this, simply drag the <code class="cCode">clock</code> directory from <code class="cCode">/WEB-INF/classes/</code> to the root directory in the pane labeled Contents of <code class="cCode">bookstore2</code>.</li></div><a name="wp96961"> </a><div class="pSmartList2"><li>Add the shared bookstore library. Navigate to<code class="cCode"> &lt;</code><code class="cVariable">INSTALL</code><code class="cCode">&gt;/j2eetutorial14/examples/build/web/bookstore/dist/</code>. Select <code class="cCode">bookstore.jar</code> and Click Add.</li></div><a name="wp96962"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp96963"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp96964"> </a><div class="pSmartList2"><li>Select the Servlet radio button. </li></div><a name="wp96965"> </a><div class="pSmartList2"><li>Click Next.</li></div><a name="wp96966"> </a><div class="pSmartList2"><li>Select <code class="cCode">dispatcher.Dispatcher</code> from the Servlet class combo box. </li></div><a name="wp96967"> </a><div class="pSmartList2"><li>Click Finish.</li></div></ol></div><a name="wp96971"> </a><div class="pSmartList1"><li>Add the listener class <code class="cCode">listeners.ContextListener</code> (described in <a  href="Servlets4.html#wp64218">Handling Servlet Life Cycle Events</a>).</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp96973"> </a><div class="pSmartList2"><li>Select the Event Listeners tab.</li></div><a name="wp96974"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp96975"> </a><div class="pSmartList2"><li>Select the <code class="cCode">listeners.ContextListener</code> class from drop down field in the Event Listener Classes panel.</li></div></ol></div><a name="wp96976"> </a><div class="pSmartList1"><li>Add the aliases.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp96977"> </a><div class="pSmartList2"><li>Select the <code class="cCode">Dispatcher</code> Web component.</li></div><a name="wp96978"> </a><div class="pSmartList2"><li>Select the Aliases tab.</li></div><a name="wp96979"> </a><div class="pSmartList2"><li>Click Add and then type <code class="cCode">/bookstore</code> in the <code class="cCode">Aliases</code> field. Repeat to add the aliases <code class="cCode">/bookcatalog</code>, <code class="cCode">/bookdetails</code>, <code class="cCode">/bookshowcart</code>, <code class="cCode">/bookcashier</code>, <code class="cCode">/bookordererror,</code> and <code class="cCode">/bookreceipt</code>.</li></div></ol></div><a name="wp96980"> </a><div class="pSmartList1"><li>Add the JSTL resource bundle basename context parameter.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp96981"> </a><div class="pSmartList2"><li>Select the Web module.</li></div><a name="wp96982"> </a><div class="pSmartList2"><li>Select the Context tab. </li></div><a name="wp96983"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp96984"> </a><div class="pSmartList2"><li>Enter <code class="cCode">javax.servlet.jsp.jstl.fmt.localizationContext</code> for the Coded Parameter.</li></div><a name="wp96985"> </a><div class="pSmartList2"><li>Enter <code class="cCode">messages.BookstoreMessages</code> for the Value.</li></div></ol></div><a name="wp96986"> </a><div class="pSmartList1"><li>Set prelude and codas for all JSP pages.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp96987"> </a><div class="pSmartList2"><li>Select the JSP Properties tab.</li></div><a name="wp96988"> </a><div class="pSmartList2"><li>Click the Add button next to the Name list.</li></div><a name="wp96989"> </a><div class="pSmartList2"><li>Enter <code class="cCode">bookstore2</code>.</li></div><a name="wp96990"> </a><div class="pSmartList2"><li>Click the Add button next to the URL Pattern list.</li></div><a name="wp96991"> </a><div class="pSmartList2"><li>Enter <code class="cCode">*.jsp</code>.</li></div><a name="wp96992"> </a><div class="pSmartList2"><li>Click the Edit button next to the Include Preludes list.</li></div><a name="wp96993"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp96994"> </a><div class="pSmartList2"><li>Enter <code class="cCode">/template/prelude.jspf</code>.</li></div><a name="wp96995"> </a><div class="pSmartList2"><li>Click OK.</li></div><a name="wp96996"> </a><div class="pSmartList2"><li>Click the Edit button next to the Include Codas list.</li></div><a name="wp96997"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp96998"> </a><div class="pSmartList2"><li>Enter <code class="cCode">/template/coda.jspf</code>.</li></div><a name="wp96999"> </a><div class="pSmartList2"><li>Click OK.</li></div></ol></div><a name="wp97000"> </a><div class="pSmartList1"><li>Add a resource reference for the database.</li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp97001"> </a><div class="pSmartList2"><li>Select the Resource Refs tab.</li></div><a name="wp97002"> </a><div class="pSmartList2"><li>Click Add.</li></div><a name="wp97003"> </a><div class="pSmartList2"><li>Enter <code class="cCode">jdbc/BookDB</code> in the Coded Name field.</li></div><a name="wp97004"> </a><div class="pSmartList2"><li>Accept the default type <code class="cCode">javax.sql.DataSource</code>.</li></div><a name="wp97005"> </a><div class="pSmartList2"><li>Accept the default authorization <code class="cCode">Container</code>.</li></div><a name="wp97006"> </a><div class="pSmartList2"><li>Accept the default selected <code class="cCode">Shareable</code>.</li></div><a name="wp97007"> </a><div class="pSmartList2"><li>Enter <code class="cCode">jdbc/BookDB</code> in the JNDI name field of the Sun-specific Settings frame.</li></div></ol></div><a name="wp97008"> </a><div class="pSmartList1"><li>Select File<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Save.</li></div><a name="wp97009"> </a><div class="pSmartList1"><li>Deploy the application. </li></div><div class="pSmartList2"><ol type="a" class="pSmartList2"><a name="wp97010"> </a><div class="pSmartList2"><li>Select Tools<span style="font-family: Symbol"><img src="images/arrwrite.gif" border="0" alt="Right Arrow"></span>Deploy. </li></div><a name="wp97012"> </a><div class="pSmartList2"><li>Click OK.</li></div></ol></div><a name="wp78708"> </a><div class="pSmartList1"><li>Open the bookstore URL <code class="cCode">http://localhost:8080/bookstore2/bookstore</code>. Click on the Start Shopping link and you will see the screen in <a  href="JSPIntro3.html#wp78717">Figure 12-2</a>:</li></div><a name="wp78715"> </a><p class="pBodyRelative"><br /><img src="images/bookcatalog12.gif" height="454" width="380" alt="Duke's Bookstore Book Catalog" border="0" hspace="0" vspace="0"/><br /></p></ol></div><p>  <a name="78717"> </a><strong><font >Figure 12-2    Book Catalog</font></strong></p><a name="wp67422"> </a><p class="pBody">See <a  href="Servlets3.html#wp71209">Troubleshooting</a> for help with diagnosing common problems related to the database server. If the messages in your pages appear as strings of the form <code class="cCode">??? </code><code class="cVariable">Key</code><code class="cCode"> ???</code>, the likely cause is that you have not provided the correct resource bundle basename as a context parameter.</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="JSPIntro2.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="JSPIntro4.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 + -