Difference Between Struts 1 and Struts 2 in Java

In this article you will learn some differences between Struts 1 and Struts 2 and some new features added to Struts 2.

Struts 1

 
It is a flexible control layer based on technologies like Java Beans, XML, Java Servlets and so on. This framework helps you to create an extensible type of development environment for applications based on standards and proven design patterns.
 

Struts 2

 
It is an extensible, elegant type of framework that is very useful for creating enterprise-ready web applications. This framework is designed for the complete cycle, in other words from building to deploying and to maintaining applications on time. It was originally known as web work 2.
 

Comparing Struts 1 and Struts 2 :

  1. The very first major difference between Struts 1 and Struts 2 is Action Classes. In Struts 1 it mandatory to extend an abstract base class. This is also a very common problem in Struts 1; that is, it does programming to the abstract classes instead of the interfaces. In Struts 2 it may implement an Action interface along with various other interfaces to facilitate custom and optional services. But it is not mandatory to use Action interfaces, we can use any POJO object with an execute signature as an Action object.
     
  2. In Struts 1 the Actions are singletons so there must be thread safety issues because there will only be one instance of a class responsible for handling all the requests for that Action. So Action resources must be thread-safe or synchronized. In Struts 2 Action objects are instantiated for each and every request so thread safety is not a big issue in Struts 2.
     
  3. Other main difference between Struts 1 and Struts 2 is configuration files. To configure Struts 1 we use struts-config.xml and in Struts 2 we can use multiple configuration files, most commonly struts.xml.
     
  4. Another difference between Struts 1 and Struts 2 is Servlet dependency. The Struts 1 action has dependencies on the Servlet API because the HttpServletRequest and HttpServletResponse are passed to the execute method when an action is invoked. In Struts 2 Actions can be tested in isolation and the other architectural elements eliminate any need to access HttpServletRequest and HttpServletResponse.
     
  5. The next point is Testability. There is a major hurdle in testing Struts 1 Actions, that is that the execute method in Struts 1 exposes the Servlet API. An extension Struts Test Case gives a set of mock objects. In Struts 2 Actions can be tested easily just by instantiating the Actions,  invoking methods and setting properties.
     
  6. Struts 1 incorporates JSTL, hence it uses the JSTL EL. The EL has a weak index and collection property support but it has basic object graph traversal. Struts 2 uses JSTL, but it also supports a more flexible and powerful expression language called Object Graph Notation Language (OGNL).
     
  7. In Struts 1 we use an ActionForm object to capture input. Here all ActionForms must extend a base class. The developers creates redundant classes to capture input because other JavaBeans cannot be used as ActionForms. We can use DynaBeans as an alternative for creating conventional Action classes, but they're also the developers are redescribing existing JavaBeans. In Struts 2 we use Action properties as input properties, eliminating the need for a second input object. The Action properties can be accessed using taglib from the webpage. Struts 2 also supports POJO Actions and POJO form objects.
     
  8. In Struts1 for binding objects into the page, we use JSP. In Struts 2 we use a technology named ValueStack technology for the same purpose. This technology allows reuse of views across a range of types that may have different property type but the same property name.
     
  9. The next important difference is a type of conversion. In Struts 1 we use Commons Beanutils for type conversion but Struts 2 uses OGNL for type conversion. Struts 1 uses per class converters and Struts 2 uses for the basic and common object type and primitives.
     
  10. In Struts, 1 manual validation is supported via a validate method on the Action form or using the extension to the common validator. In Struts 2 manual validation is supported via the validate method and the XWork validation framework.
     
  11. For each module Struts, 1 supports separate request lifecycles but Struts 2 supports various lifecycles per Action basis. In Struts 1 all Actions in the module should share the same lifecycles on the other hand Struts 2 can create custom stacks and use it with various actions.
     
  12. In Struts 1 the front controller is Action Servlet but in Struts 2 the front controller is Filter Dispatcher.
     
  13. Struts 1 has multiple tag libraries like HTML, Bean and so on. Struts 2 does not have multiple tag libraries, it has a single library.

     Clipboard02.jpg
There are many new features added to Struts 2. Some of them are :
  • Struts 2 introduced an AJAX theme to make more dynamic web applications.
     
  • It supports configuration of Action classes using Annotations.
     
  • It easily incorporates with Spring and Hibernate.
     
  • It has a more simplified design.
     
  • It simplifies testability.
     
  • In Struts 2 checkboxes do not require special handling for false values.
     
  • No manual configuration is required in Struts 2, the extensions can be added by just dropping in a jar.
     
  • It has a great support for multiple view options like Freemarker, JSP, Velocity and so on.
     
  • It is easy to modify tags in Struts 2.