|
Sample J2EE Applications |
SilverStream provides the SilverBooks sample application to illustrate the Model-View-Controller design pattern. This page describes the SilverBooks application. It covers the following topics:
SilverBooks is a Web-based e-commerce application that simulates an online bookstore. Users can interact with this application to purchase books from SilverBooks Corporation, a fictitious bookseller.
SilverBooks is implemented using J2EE standards and the Struts Framework architecture. Struts Framework is an open-source framework for Web applications that implements the Model-View-Controller (MVC) design paradigm. As an application that conforms to this paradigm, SilverBooks includes the following components:
The major benefit of this approach is that it logically separates the key functions of the application--actions, data access, and presentation--to allow role-specific assignments, ease of maintenance, and efficient debugging.
SilverBooks incorporates J2EE standard technologies to implement the Struts Framework and Model-View-Controller (MVC) design paradigm. The following technologies are described:
For more information about Struts Framework and the MVC design paradigm, see the Struts Framework Project Web site.
JavaServer Pages (JSPs) are used to implement the View component of the MVC design paradigm. SilverBooks uses JSPs to present both the static and the dynamic content of the bookstore application.
Static The following JSPs display data that changes infrequently if at all in SilverBooks:
In fact, because the navigation bars never change, these two JSPs are reused throughout the application.
Dynamic A more dynamic use of JSPs in SilverBooks is for presenting details about a book that has been selected from search results by the end user. When a user clicks a book title hyperlink, control passes to a JSP called bookdetail.jsp. This JSP in turn calls properties on a JavaBean called BookBean.java to determine how to present data about the book the user selected.
For more information about JSPs, see the chapter on using JavaServer Pages in the Programmer's Guide of the server's Classic Development Help.
The action servlet is the cornerstone of the Controller component of the MVC design paradigm. In SilverBooks, the action servlet performs three key functions:
Receives requests from the client--in this case, a user interacting with the SilverBooks application in a Web browser
Delegates to an appropriate View component the responsibility for producing the next part of the user interface
Two XML specifications are used to configure the action servlet. The specification in web.xml associates the URL pattern *.do with the action servlet. This association means that any URL request or post ending in .do will go to the action servlet for processing. The specification in action.xml maps each possible client request to an action class that performs the appropriate business logic or handles the possible exceptions.
For example, here is one of the URL mappings in action.xml:
<action path="/bookDetail" actionClass="com.sssw.demo.silverbooks.action.DetailAction"> <forward name="success" path="/bookdetail.jsp" /> </action>
This mapping means that when the URL bookDetail.do is requested, the action servlet instantiates DetailAction class (or uses one that has already been instantiated) to display the detail data about a book selected by the user. The book identifier is passed as a parameter on the URL. This action class has access to a forward-mapping address called success which is mapped to the relative URL bookdetail.jsp.
The DetailAction class services the client request as follows:
Gets a reference to the book in the EJB data source, based on the book identifier. Each book is represented by a book bean
If no exceptions occur, passes control to the JSP page bookdetail.jsp, which calls properties on the book bean to determine how to display the page
The shopping cart works in a similar way. When a user decides to add a book to the shopping cart, the URL cart.do is requested, causing the action servlet to pass control to the AddCartAction class along with two parameters on the URL: add as the action, and the book identifier.
Form classes represent the Model component of the MVC design paradigm. For example, the check-out and account detail pages use the form classes PaymentForm.java and AccountDetailForm.java to store data about customers' orders. When the data is updated, the application calls the validate() method on these form classes to ensure the accuracy of the information.
Enterprise JavaBeans (EJBs) represent business logic and manage data access in the MVC paradigm. EJBs are referenced by action servlets and form classes.
In SilverBooks, EJBs perform two key functions:
Retrieve and maintain book data from an external JDBC data source. An EJB session bean interacts with this database, which contains pertinent data about each book (including book identifier, title, author, description, price, publisher, and ISBN number).
Manage the shopping cart. An EJB session bean keeps track of shopping-cart values from page to page. Two entity beans are used to process orders.
The SilverStream server provides the connections to these databases.
For more information about EJBs, see the chapter on using EJBs with SilverStream applications in the Programmer's Guide of the server's Classic Development Help.
The Struts Framework provides a methodology for internationalizing prompts and messages in a Web application. SilverBooks implements this feature by using Java resource bundles that use name/value pairs to translate static text labels to the language selected by the user. Each language supported by SilverBooks has its own resource bundle.
Start the SilverBooks application by entering the following URL in your browser for Sybase Adaptive Server Anywhere databases:
http://server/SilverBooks/app
or for Informix Cloudscape databases, enter this URL:
http://server/SilverBooksCS/app
Select the radio button associated with your language of choice and click Go.
The page refreshes to display static text labels in the language you selected.
The SilverBooks sample application includes source code, ready-to-deploy archives and databases with the archives already installed. You'll find these files in the SilverStream\samples\SilverBooks directory. You'll notice that there is a set of files for the Informix Cloudscape database in the \Cloudscape subdirectory and a set of files for the Sybase Adaptive Server Anywhere database in the \SybaseASA subdirectory. There is also a directory containing the source files.
You can run either version of the application by adding the appropriate SilverBooks database to the SilverStream server.
About the other files provided The Sybase ASA and Cloudscape subdirectories also include these files:
You can use run these files if you make changes to the application or want to run with debug on. For more information on using these files, see Modifying the SilverBooks databases
After installing the SilverBooks database, open your browser.
Enter this URL for the Sybase Adaptive Server database version:
http://server/SilverBooks/app
Use this URL for the Cloudscape database version:
http:///server/SilverBooksCS/app
The home page opens in your browser:
Interact with the SilverBooks application by:
In most cases, you will only need to run the application as described in Getting started. If you want to work with the database by either of the following:
you need to rebuild and redeploy the application.
Open a command prompt and change to the following directory for Cloudscape:
%SilverStream_install_directory%\samples\SilverBooks\Cloudscape
or this directory for Sybase Adaptive Server Anywhere:
%SilverStream_install_directory%\samples\SilverBooks\SybaseASA
buildSilverBooksAll
The buildSilverBooksAll command should not generate any errors.
Now you redeploy the application.
Open a command prompt and change to the following directory for Cloudscape:
%SilverStream_install_directory%\samples\SilverBooks\Cloudscape
or this directory for Sybase Adaptive Server Anywhere:
%SilverStream_install_directory%\samples\SilverBooks\SybaseASA
deploysilverbooksear %ServerName%:%Port%
|
Sample J2EE Applications |
Copyright © 2001, SilverStream Software, Inc. All rights reserved.