Monday, 18 April 2016

Difference between Struts 1 and Struts 2 framework GNIIT SOLUTION mkniit.blogspot.in

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 usinDependency 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 returnsActionForward 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 usingstruts-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 iweb.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.

No comments:

Post a Comment