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. |
Next: Creating the JSP Pages >>
More Java Articles
More By McGraw-Hill/Osborne