Wednesday, March 13, 2013

Difference between Struts 1 and Struts 2 framework

I had work previously on Struts 1 but never touched Struts 2, specially since Spring MVC was there to take the leading role. Recently one of my friend ask me to help with Struts2, which leads me to look on 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 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 difference between Struts 1 and Struts 2 is a good candidate to include in my list of Struts interview question 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 Struts 2 framework. Struts 2 has also done good job on removing direct dependency of Action classes on Servlet API e.g. HttpServletRequest and HttpServletResponse, which makes testing easy by using 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) 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) 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 file, most commonly used as struts.xml. What is more important is 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 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 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 required Servlet API.

There are lot more difference in Struts 1 and Struts 2 and I suggest going thorough 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

6 comments :

Anonymous said...

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

Jarek said...

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

Anonymous said...

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

Basavaraj Dapalapur said...

nice docx

Satishkumar Abburi said...

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

Satishkumar Abburi said...

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

Post a Comment