Coding and Unit Testing:

web.xml
The deployment descriptor for the Web application. It has an entry for the struts ActionServlet which the generic Controller. It also maps all *.do extension through the ActionServlet. The struts-config.xml should be available in the path specified relative to the web application context.

<web-app>
   <display-name>Struts Shopping Cart Application</display-name>

   <!-- Standard Action Servlet Configuration -->
   <servlet>
      <servlet-name>action</servlet-name>
      <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
      <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
      </init-param>
      <load-on-startup>2</load-on-startup>
   </servlet>

    <!-- Standard Action Servlet Mapping -->
    <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
     </servlet-mapping>

 </web-app>


struts-config.xml:

For our application we just need to declare the ActionForm and  add an ActionMapping.

<form-beans>
    <form-bean
        name="CartItemForm"
                    type="in.techfreaks.struts.cart.form.CartItemForm"/>
</form-beans>

<action-mappings>
    <action path="/Cart" type="in.techfreaks.struts.cart.action.CartAction" name="CartItemForm">
           <forward name="success" path="/ShoppingCart.jsp" />
    </action>
</action-mappings>



The action maps the Path to the Action class. It also has a forward to ShoppingCart.jsp on success. We do not have a failure forward but it would be required for a more robust production quality application. The ActionForm associated with the request is also provided. It maps to the ActionForm defined by the form-bean tag. The path when called from the JSP would call the Action class. Note that in the path which is in the action mapping, we do not have .do added but when we declare in the html form action, we would have the Cart.do.

CartAction :

The below code is excerpt from the CartAction class. The CartItemForm is passed in as a parameter to execute() method. Also, we moved the action into the CartItemForm. The CartItemForm is automatically populated by the Struts framework. We just need to make sure that the parameter name in the ActionForm is exactly same as the html form input fields in JSP.

public class CartAction extends Action {

   public static final String ADD = "Add";
   public static final String DELETE = "Delete";
   public static final String UPDATE = "Update";

   public static final String CART_SESSION = "Cart";

   public ActionForward execute(ActionMapping mapping, ActionForm form,
           HttpServletRequest request, HttpServletResponse response) throws Exception {
        CartItemForm cartItemForm = (CartItemForm) form;
        String strAction = cartItemForm.getAction();
        if(strAction!=null && !strAction.equals("")) {
          if(strAction.equals(ADD)) {
              addToCart(request, cartItemForm);
          } else if (strAction.equals(UPDATE)) {
              updateCart(request, cartItemForm);
          } else if (strAction.equals(DELETE)) {
              deleteCart(request, cartItemForm);
          }
   }

   return mapping.findForward("success");
}

CartItemForm:

Below is an excerpt of CartItemForm. Note that we moved itemIndex and action fields into the CartItemForm and hence we do not used the request.getParameter() to fetch these values in Action class, like we did the previous version of the controller.

 public class CartItemForm extends ActionForm {
    private String strPartNumber;
    private String strModelDescription;
    private double dblUnitCost;
    private int iQuantity;
    private double dblTotalCost;
    private String action ;
    private String itemIndex ;
}
 
ModelList.jsp and ShoppingCart.jsp:

The main change is calling the Action class instead of the Servlet. Below code explains how the Action class is called:

<form name="modelDetail3" method="POST" action="Cart.do">

Apart from that the JSP needs to be modified to use the web 2.0 div structure and have external CSS applied. However, we would defer to you as a home work ;)

 
Putting the cart to test:
We mainly want to test the below three functionalities:
1. Add to cart
2. Update the quantity
3. Remove an item from cart

Start Tomcat and access URL http://localhost:8080/StrutsShoppingCart/ModelList.jsp to test the above functionality.

You can download the WAR file by clicking StrutsShoppingCart download application link.

 

This WAR file can be directly deploy into Tomcat Web container. Adding the WAR to the webapps folder of Tomcat will automatically perform a Hot deployment.

This concludes the development of session based shopping cart using Struts 1 Framework.

We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.