Purpose

The purpose of this workshop is to migrate the shopping cart which we created in Session Based Shopping Cart Article and use Struts 1 framework for MVC. The article is not intended to explain all struts features like localization, validation framework, error handling etc.

Prerequisites

  • Knowledge about struts1 application framework
  • This articles assume that a session based shopping cart is already developed as explained in Session Based Shopping Cart Article . All requirements are same as the previous article.

Analysis and design

Our analysis and design will not be as exhaustive as the previous session based shopping cart. We already have the components designed and responsibility assigned (Also have code available). We will use the previous components and review the changes required.

Based on the previous article below are the list of components:
1. ModelList.jsp
2. ShoppingCart.jsp
3. CartBean.java
4. CartItemBean.java
5. CartController.java


We will move most of the logic from CartController which was a servlet and implement a Struts Action class instead. Hence, we create a new class CartAction which extends Action class.

If you look at the ModelList or ShoppingCart form(HTML Form), each form submits details of an Item or Model. ModelList has Add action on Item and ShoppingCart.jsp has Update and Delete action on Item. Based on this information, we can modify the CartItemBean into CartItemForm which extends Struts ActionForm class. Instance of the ActionForm will be provided as input to CartAction.

Hence, the revised list of components would look something like:
1. ModelList.jsp
2. ShoppingCart.jsp
3. CartBean.java
4. CartItemForm.java
5. CartAction.java

 

Development environment structure:

I prefer starting a struts 1 application by publishing the struts-blank application to Tomcat. The struts-blank application can be download from Apache website. The published struts-blank application provides a sample of struts-config.xml and all the required libraries to run a struts application.

However, at the end of the application we have provided a link to down the Web Archive (WAR) file of the whole StrutsShoppingCart application (with Source code).

StrutsShoppingCart
  - ModelList.jsp
  - ShoppingCart.jsp
     WEB-INF
      - struts-config.xml
      - web.xml
      src
        - in.techfreaks.struts.cart.bean.CartBean.java
        - in.techfreaks.struts.cart.form.CartItemForm.java
        - in.techfreaks.struts.cart.action.CartAction.java
      classes
      lib
      - jstl.jar
      -standard.jar
      - <ALL Struts required Lib >


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.