Sunday, July 25, 2021

Difference between Struts 1 and Struts 2 Web Development Framework

I had worked previously on Struts 1 but never touched Struts 2, especially since Spring MVC was there to take the leading role. Recently one of my friends ask me to help with Struts2, which leads me to look on the Struts2 framework from start. First thing I wanted to find out differences between Struts 1 and Struts 2 framework, because in my experience, if you have worked in the previous version looking differences between two versions of Struts can quickly help to find, what changes and What are the new features, concepts and improvement is offered by Struts 2. Also the difference between Struts 1 and Struts 2 is a good candidate to include in my list of Struts interview questions for quick revision.

To my surprise, Struts 2 seems to be completely different than Struts 1 framework, because some of the most familiar stuff like ActionForm, struts-config.xml, and Action classes are changed in the Struts 2 framework. 

Struts 2 has also done great job on removing the direct dependency of Action classes on Servlet API e.g. HttpServletRequest and HttpServletResponse, which makes testing easy by using the Dependency Injection concept. In this article, we will some important differences between Struts 1 and Struts 2 framework.



Struts 1 vs Struts 2 - Differences

Difference between Struts 1 and Struts 2 framework Java J2EE MVCHere is my list of some common difference between Struts 1 and Struts 2 framework. This list contains some observations, which can also help you to gauge some major changes in struts 2 from struts 1.


1) The first major difference between  Jakarta Struts 1 and Struts 2 framework is in Action class itself. In Struts 1 it's mandatory to extend org.apache.struts.action.Action and  implement execute() method which returns ActionForward and accept HttpServletRequest and HttpServletResponse

This is not the case with Struts 2, here Action class can be a simple POJO or Java object with execute() method. 

Also, execute() method returns String rather than returning ActionForward object. You can still use  ActionSupport class or Action interface but those are completely optional.



2) The second main difference between Struts 1 and Struts 2 is on configuration files, earlier we used to configure Struts using struts-config.xml, but with Struts 2 you can use multiple configuration files, most commonly used as struts.xml. What is more important is the declaration of Struts 2 Filter in web.xml e.g.

<filter>
      <filter-name>struts2Fitler</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher

      </filter-class>

</filter>

<filter-mapping>
      <filter-name>struts2Fitler</filter-name>
      <url-pattern>/*</url-pattern>
</filter-mapping>
 
Also, if you notice, instead of mapping this to *.do or *.action we have mapped it with *, which means all url pattern will be flown to struts2 filter.

3) One more difference I noticed between Struts 1 and Struts 2 is on Front end Controller. In Struts 1, ActionServlet is considered as FrontController while in Struts 2 its Filter, which can be considered as front end controller.

4) Another useful enhancement in Struts2 is Interceptor API, which allows to do a lot of stuff much easily e.g. file upload using Struts2's builtin FileUploadInterceptor class.

5) One more difference between Struts 1 and Struts 2 which I like is removing the dependency of Action classes to Servlet API in form of HttpServletRequest and HttpServletResponse classes required in execute() method. Struts 2 don't have such dependency and its execute() method doesn't require Servlet API.

There are a lot more differences in Struts 1 and Struts 2 and I suggest going through Struts 2 documentation as you learn Struts 2. To me, Struts 2 looks completely different than Struts 1. Since I am using Spring MVC more frequently, I still need to explore Struts 2 more closely. I will keep this list of differences between Struts 1 and Struts 2 updated as and when I found some more differences.


Other J2EE tutorials from Javarevisited Blog
How to control concurrent active user sessions in Java web application using Spring Security

7 comments:

  1. Hi,
    Can you write some article,
    listing new component libraries, added on top of various MvcWebFrameworks
    which is very easy/quick to use, and has many different object types.
    My company is stucked with Struts1 and commonControls, and this is very hard to convince that bureaucracy...

    one more thing.
    is there any chance to change email, and send full page content, not just the first paragraph.
    im very happy spending 10 minutes readying your posts, but this colourful page is a problem in openspace

    ReplyDelete
  2. I think the main difference is that Struts 1 is dead in terms of development :)

    ReplyDelete
  3. In Struts 1 We can also define multiple struts-cinfig.xml file.

    ReplyDelete
  4. In struts 1.x an Action class is a single ton class, so Action class object is not a thread safe, as a programmer we need to make it as thread safe by applying synchronization

    In 2.x an Action class object will be created for each request, so it is by default thread safe, so we no need to take care about safety issues here

    ReplyDelete
  5. In struts 1.x we have only jsp as a view technology

    In 2.x we have support of multiple view technologies like velocity, Freemarker etc

    ReplyDelete
  6. struts1.x doesn't support Annotations concept.but 2.x support the annotaions.
    and in struts1.x ActionServlet will create the mapping obj,form obj.but in struts2.x its not possible

    ReplyDelete