10 difference between Struts and Struts2 framework in Java

What is the difference between Struts 1 and Struts 2 is one of the popular Struts interview questions from Java JEE interviews? If you have mentioned Struts in your CV then you can expect this question on the telephonic or face-to-face round of interview. Struts 1.x was a very popular MVC framework for Java web application on last decade, but when Spring MVC comes it lost a lot of its sheen. Spring framework's dependency injection and inversion of control feature, along with several other template based features makes it the de-facto framework for developing Java application. But, the demand of Struts was not completely gone, there are still a lot of enterprise application written in Struts which needs Java developer with knowledge of Struts to maintain and develop them, so it's one of the good skill to get a Java web developer job. Apache than comes with Struts 2, the new version of Struts which supports dependency injection and several other features. In this article, I'll show you some key difference between Struts 1 and Struts 2 from interview point of view, but if you are learning Struts 2 by yourself and looking for a good book than buy Struts 2 in Action, one of the best books to learn Struts 2.



Difference between Struts 1.x vs Struts 2.x framework

Struts2 is designed to overcome the shortcomings of Struts1 and to make it more flexible, extendable. Some of the noticeable differences are:


Front Controller
One of the key structural In Struts 1.x ActionServlet acts as a front controller and for each request, a new form bean instance is created to hold parameter that is accessed by Action class, while in Struts 2.x FilterDispatcher acts as a front controller. As request received a new instance of action class is created and param interceptor load all the parameter from the request to the field of action instance.


Action Classes
Another fundamental difference between Struts 1.x and Struts 2 framework is that Struts1's Action classes are forced to extend an abstract class that makes it not extendable. Struts2's action classes are more flexible and we can create them by implementing Action interface, extending ActionSupport class or just by having executed() method.


Thread Safety 
This is another important difference between Struts 1 and Struts 2 which is often overlooked by Java developers. Struts1 Action Classes were Singleton and not thread safe, that means extra care was required from developer side to avoid any side effects because of multithreading. Struts2' action classes get instantiated per request, so there is no sharing and multithreading, hence they are thread safe.


Servlet API coupling
One of the bigger and framework level difference between Struts 1 and Struts 2 is that Struts 1 APIs are tightly coupled with Servlet API and Request and Response objects are passed to action classes' execute() method. On the other hand, Struts2 API is loosely coupled with Servlet API and automatically maps the form bean data to action class java bean properties that we mostly use. If however we need a reference to Servlet API classes, there are *Aware interfaces for that.


Testing 
This difference is one of the most crucial difference as long as code quality and productivity is a concern. Struts1 action classes were hard to test because of Servlet API coupling, but Struts2 Action classes are like normal Java classes and we can test them easily by instantiating them and setting their properties.


Request Parameters mapping
One more interesting difference between Struts 1 and Struts 2 is that Struts1 requires us to create ActionForm classes to hold request params and we need to configure it in the struts configuration file. Struts2 request params mapping is done on the fly and all we need is to have java bean properties in action classes or implement ModelDriven interface to provide the java bean class name to be used for mapping.


Tag Support
Another functional difference between Struts 1 and Struts 2 is that Struts1 uses JSTL Tags and hence are limited. Struts2 uses OGNL and provide different kinds of UI, Control and Data Tags. It’s more versatile and easy to use.


Validation
One more useful difference between Struts 2.x and Struts 1.x is that Struts1 supports validation through manual validate() method Struts2 support both manual validation as well as Validation framework integration.


Views Rendering 
The 9th difference between Struts 1.x and Struts 2.x version are that Struts1 uses standard JSP technology for providing bean values to JSP pages for views. Struts2 uses ValueStack to store request params and attributes and we can use OGNL and Struts2 tags to access them.


Modules support
Last but not the least difference between Struts 1.x and Struts 2.x is that Struts1 modules are complex to design and look like separate projects Struts2 provides “namespace” configuration for packages to easily create modules.

So, these were 10 key difference between Struts 1.x and Struts2.x web MVC framework. Here is a nice summary of some more difference between Struts 1 and Struts 2.x framework from Apache Struts website:
Difference between Struts1 and Struts2 framework


That's all about the difference between Struts and Struts 2.0 web MVC framework in Java. Even though Struts 2 seems an extension of Struts 1, it's a completely different web MVC framework. It's more similar to Spring MVC than Struts 2 itself.

Other Sturts articles, tutorials and interview questions you may like:
  • How to get ServletContext in Struts Action Class? (answer)
  • 10 examples of the display tag with Struts in Java? (article)
  • 3 Free Struts Books for Java Developers? (see here)
  • More differences between Struts 1.x and Struts 2.x in Java (answer)

Further Reading
Struts 2 in Action By Don Brown


No comments:

Post a Comment