January 17, 2021
Hot Topics:

Comparing the Struts 1 and Struts 2 Web Application Frameworks

  • By Michael Klaene
  • Send Email »
  • More Articles »

Compared to the original DefectsList Action class, I think you will agree the code here is much cleaner. Extending the Struts 2 ActionSupport class is not a requirement but it does provide access to many useful methods and constants, such as the SUCCESS constant you are using as a result. Contributing to the leanness of this code is the fact tha the execute method, which used to require four parameters, now has zero parameters. One of these parameters was for the ActionForm and ActionForms that are no more. But besides that, the remaining parameters are missing because Struts 2 uses the Spring framework internally to inject necessary dependencies like the HTTPServletRequest object at runtime, when it is needed. Along with dependency injection, an important concept in Struts 2 is its use of Interceptors. Essentially, Interceptors are Java classes provided by the Struts 2 framework that allow you to perform logic before and after Action executions. Several Struts 2 Interceptors are provided out of the box, but you can also create your own. Typically, to use an Interceptor, an Action class will implement the appropriate interface; then, this will alert the Interceptor associated with that interface that it has a job to do.

Because Spring is built into Struts 2, injecting Spring beans is as easy as creating a setter method for the Spring-managed bean. Struts 2 will inject this dependency automatically at runtime. Another option available is to declare Struts Action classes as Spring beans in Spring's configuration file. Read the Struts 2 documentation for more information on this.

Here is the result of your Struts 2 'list' action, defects.jsp. The Struts 2 tag library is easier to use and much more powerful than those provided in Struts 1. One noteworthy feature of the tag library is its use of themes. Depending on which theme you set, the tags will generate a good deal of stylized markup for you, further reducing the amount of page clutter. You can set the theme globally and can override it for individual tags, as I have done here to keep the generated output similar to the Struts 1 version of defect-tracker. Here are a few examples of Struts 2 tags found in the list page:

<!--passes id as parameter to the input method on our Action-->
<s:url id="editLink" action="action!input">
   <s:param name="id" value="%{id}" />

<!--Displays a formatted date as a label-->
<s:date name="submittedOn" format="yyyy-MM-dd hh:mm" />

DefectsAction in Struts 2 looks even cleaner than DefectsList given the number of methods it contains. There are no ActionForms to contend with, no lengthy method APIs, and you can access your Spring business layer with ease, just as you did in DefectsList. Here is the code in its entirety:

package web;

import business.DefectsManager;
import business.data.Defect;
import com.opensymphony.xwork2.ActionSupport;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;

public class DefectsAction extends ActionSupport
                           implements ServletRequestAware {

   private Defect defect;
   private DefectsManager defectsManager;
   private HttpServletRequest request;

   public Defect getDefect() {
      return defect;

   public void setDefect(Defect defect) {
      this.defect = defect;

   public void setDefectsManager(DefectsManager defectsManager) {
      this.defectsManager = defectsManager;

   public void setServletRequest(HttpServletRequest
                                 httpServletRequest) {
      this.request = httpServletRequest;

    public String input() {
      if (request.getParameter("id") != null) {
         long id = Long.parseLong(request.getParameter("id"));
         this.defect = this.defectsManager.getDefect(id);
      return INPUT;

   public String delete(){
      long id = Long.parseLong(request.getParameter("id"));

      return SUCCESS;

   public String save() {

      return SUCCESS;


As with Struts 1, you need to access the HTTPServletRequest to obtain the defect's IF. To obtain this ID, DefectsAction utilizes what is probably the most common Struts 2 Interceptor you will use. You simply implement the ServletRequestAware interface and add a setter method for the request and the framework will make sure you have it when you need it. The 'input' method in this class replaces the add and edit methods in the previous version of the application. If an ID is present, you query for the defect; otherwise, you simply continue on to the result. This leads you to editDefect.jsp. In Struts 2, if the page is passed a reference to a null object, it will automatically create a new one. This is exactly what you want when creating a new Defect object.

Page 5 of 6

This article was originally published on November 8, 2007

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date