Home arrow Java arrow Page 8 - Online Store Application
JAVA

Online Store Application


If you want to build an online store application using Java, look no further. This article begins with an overview of the application, and then discusses the applicatin development process. It is taken from chapter 14 of JavaServer Faces Programming by Budi Kurniawan (McGraw-Hill, 2003; ISBN 0072229837).

Author Info:
By: McGraw-Hill/Osborne
Rating: 5 stars5 stars5 stars5 stars5 stars / 54
March 16, 2005
TABLE OF CONTENTS:
  1. · Online Store Application
  2. · Understanding the Page Control Flow
  3. · Structuring the Database Tables
  4. · The DatabaseUtil Class
  5. · The MenuBean Class
  6. · The ShoppingItemBean Class
  7. · Registering the Beans in the Application Configuration File
  8. · Adding the ActionListener
  9. · Creating the JSP Pages
  10. · The search.jsp Page
  11. · The shoppingCart.jsp Page

print this article
SEARCH DEVARTICLES

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.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials