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).
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.
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.
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.
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.
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-16The 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">
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.