Java
  Home arrow Java arrow Page 8 - Online Store Application
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Online Store Application
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 21
    2005-03-16

    Table of Contents:
  • Online Store Application
  • Understanding the Page Control Flow
  • Structuring the Database Tables
  • The DatabaseUtil Class
  • The MenuBean Class
  • The ShoppingItemBean Class
  • Registering the Beans in the Application Configuration File
  • Adding the ActionListener
  • Creating the JSP Pages
  • The search.jsp Page
  • The shoppingCart.jsp Page

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Online Store Application - Adding the ActionListener


    (Page 8 of 11 )

    The application uses an ActionListener called AppActionListener. In our application, the ActionListener’s processAction method is invoked when the user clicks the Buy button in the product details page and when the user clicks the Pay button in the check out page. Two private methods, getValueBinding and getDatabaseUtil, are used from various points in the AppActionListener class. The getValueBinding method returns a ValueBinding object for the specified value reference. The getDatabaseUtil method returns the DatabaseUtil instance in ServletContext.

    The AppActionListener class is shown in Listing 14-15.  

    Listing 14-15  The AppActionListener Class

    package buydirect;

    import java.util.Iterator;
    import javax.faces.FactoryFinder;
    import javax.faces.application.Application;
    import javax.faces.application.ApplicationFactory;
    import javax.faces.context.FacesContext;
    import javax.faces.el.ValueBinding;
    import javax.faces.event.ActionEvent;
    import javax.faces.event.ActionListener;
    import javax.faces.event.PhaseId;
    import javax.faces.component.UICommand;
    import javax.faces.component.UIComponent;
    import javax.faces.component.UIInput;
    import javax.faces.tree.Tree;
    import javax.servlet.ServletContext;

    public class AppActionListener implements ActionListener {
     
    public PhaseId getPhaseId() {
        return PhaseId.INVOKE_APPLICATION;
      }

      public void processAction(ActionEvent event) {
        String actionCommand = event.getActionCommand();
        if ("buy".equals(actionCommand)) {
         FacesContext facesContext = FacesContext.getCurrentInstance();
        
    String productId = (String) facesContext.getExternalContext().
    getRequestParameterMap().get("productId");
         ShoppingCartBean cart =
          (ShoppingCartBean) getValueBinding("ShoppingCartBean").
    getValue(facesContext);
         ProductBean product = getDatabaseUtil(). getProductDetails(productId);
         ShoppingItemBean shoppingItem = new
           ShoppingItemBean(product.getId(), product.getName(), product.getPrice(), 1);
         cart.addShoppingItem(shoppingItem);
       }
       else if ("order".equals(actionCommand)) {
        
    // insert a record into the database
         FacesContext facesContext = FacesContext.getCurrentInstance();
         OrderBean order = (OrderBean)
    getValueBinding("OrderBean").getValue(facesContext);    
         ShoppingCartBean cart = (ShoppingCartBean)

    getValueBinding("ShoppingCartBean").getValue(facesContext);
         getDatabaseUtil().insertOrder(order, cart);
         // empty shopping cart
         cart.removeShoppingItems();
       }

    }

    private ValueBinding getValueBinding(String valueRef) {
      ApplicationFactory factory =
    (ApplicationFactory)FactoryFinder.
    getFactory(FactoryFinder.APPLICATION_FACTORY);
       Application application = factory.getApplication(); 
       return application.getValueBinding(valueRef);
    }

    private DatabaseUtil getDatabaseUtil() { FacesContext 
      facesContext = FacesContext.getCurrentInstance();
        ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();
        return (DatabaseUtil)
    servletContext.getAttribute("DATABASE_UTIL");
      }
    }


    The processAction method starts by obtaining the action command by invoking getActionCommand on the ActionEvent instance.

    String actionCommand = event.getActionCommand();

    When the user clicks the Buy button in the product details page, the ActionListener executes the code within the following if block:

    if ("buy".equals(actionCommand)) {
      ...
    }

    The processAction method obtains the product identifier from the Request object in the ExternalContext instance in FacesContext and assigns it to the productId String variable.

      FacesContext facesContext = FacesContext.getCurrentInstance();
        String productId = (String) facesContext.getExternalContext().
    getRequestParameterMap().get("productId");

    It then retrieves the ShoppingCartBean from FacesContext.

      ShoppingCartBean cart =
    (ShoppingCartBean) getValueBinding("ShoppingCartBean"). getValue(facesContext);

    Next, it obtains the product information by calling the getProductDetails method of the DatabaseUtil instance and uses it to create an instance of the ShoppingItemBean class.

      ProductBean product = getDatabaseUtil(). getProductDetails(productId);
      ShoppingItemBean shoppingItem = new
      ShoppingItemBean(product.getId(), product.getName(), product.getPrice(), 1);

    The processAction method then adds the ShoppingItemBean instance to the shopping cart.

    cart.addShoppingItem(shoppingItem);

    When the user clicks the Pay button in the check out page, the ActionListener executes the code within the following if block:

    else if ("order".equals(actionCommand)) {
      ...
    }

    The code within the if block inserts the order information and clears the shopping cart. It first obtains the OrderBean instance and the ShoppingCartBean instance:

      // insert a record into the database
      FacesContext facesContext = FacesContext.getCurrentInstance();
      OrderBean order = (OrderBean)
    getValueBinding("OrderBean").getValue(facesContext); 
      ShoppingCartBean cart = (ShoppingCartBean) getValueBinding("ShoppingCartBean").getValue(facesContext);

    Then it calls the insertOrder method of the DatabaseUtil instance, passing the OrderBean and ShoppingCartBean instances:

    getDatabaseUtil().insertOrder(order, cart);

    Lastly, the ActionListener calls the removeShoppingItems method of the ShoppingCartBean to clear the shopping cart.

    // empty shopping cart
    cart.removeShoppingItems();


    Writing the Deployment Descriptor

    Just like any other JSF application, the BuyDirect application needs a deployment descriptor that specifies the FacesServlet servlet and the servlet mapping. In addition, you need to declare several context-param tags and a listener for the AppContextListener class. The deployment descriptor for this application is shown in Listing 14-16.

    Listing 14-16  The Deployment Descriptor for the BuyDirect Application

      <?xml version="1.0"?>
      <!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">

     
    <web-app>
       
    <context-param>
          <param-name>dbUrl</param-name>
          <param-value>jdbc:odbc:buydirect</param-value>
     
    </context-param>
     
    <context-param>
        <param-name>jdbcDriver</param-name>
        <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
     
    </context-param>
     
    <context-param>
        <param-name>pageWidth</param-name>
        <param-value>678</param-value>
     
    </context-param>
      <context-param>
        <param-name>menuWidth</param-name>
        <param-value>155</param-value>
      </context-param>

      <listener>
        <listener-class>buydirect.AppContextListener</listener-class>
      </listener>

     
    <!-- Faces Servlet -->
      <servlet>
        <servlet-name>Faces Servlet</servllet-name>
        <servlet-class>javax.faces.webapp.FacesServlet </servlet-class>
        <load-on-startup> 1 </load-on-startup>
      </servlet>

      <!-- Faces Servlet Mapping -->
      <servlet-mapping>
        <servlet-name>Faces Servlet</servllet-name>
        <url-pattern>/faces/*</url-pattern>
      </servlet-mapping>
    </web-app>



    This article is excerpted from JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837). Check it out at your favorite bookstore today. Buy this book now.

    More Java Articles
    More By McGraw-Hill/Osborne


       · I go thru the online application, and I'm new to this topic, can anyone recreate...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT