Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

10 Difference between Struts 1.x and Struts 2.x 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 interviews. Struts 1.x was a very popular MVC framework for Java web applications in the 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 applications. 

But, the demand of Struts has not completely gone, there is 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 then comes with Struts 2, the new version of Struts that supports dependency injection and several other features. 

In this article, I'll show you some key differences between Struts 1 and Struts 2 from an interview point of view, but if you are learning Struts 2 by yourself and looking for a good book then read 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:

1. 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 loads all the parameters from the request to the field of an action instance.

2. 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 the Action interface, extending ActionSupport class, or just by having executed() method.

3. 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, which means extra care was required from the developer side to avoid any side effects because of multithreading. Struts2's action classes get instantiated per request, so there is no sharing and multithreading, hence they are thread-safe.

4. Servlet API coupling
One of the bigger and framework level differences 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.

5. 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.

6. 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 a ModelDriven interface to provide the java bean class name to be used for mapping.

7. 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 provides different kinds of UI, Control, and Data Tags. It’s more versatile and easy to use.

8. 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.

9. 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.

10. Modules support
Last but not 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 differences between Struts 1.x and Struts2.x web MVC framework. Here is a nice summary of some more differences between Struts 1 and Struts 2.x framework from the 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. While not many people are using Struts these days knowing this difference is crucial if you are going to work on a legacy struts based project in your job. 

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)

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.