Introduction
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 :
- The very first major difference between Struts 1 and Struts 2 is Action Classes. In Struts 1 it 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.
- 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.
- 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.
- 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 is passed to the execute method when an action is invoked. In Struts 2 Actions can be tested in isolation and the other architectural elements eliminates any need to access HttpServletRequest and HttpServletResponse.
- 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.
- 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).
- 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 there 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.
- 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.
- The next important difference is type 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 basic and common object type and primitives.
- 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.
- 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.
- In Struts 1 the front controller is Action Servlet but in Struts 2 the front controller is Filter Dispatcher.
- 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.
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.