Tuesday, June 1, 2010

Differences Between Struts1 and Struts2

                                                                                                                             -Dhanapathi Marepalli 
In this section we will be discussing  major differences between Struts1 and Struts 2.

1. Action Classes:- In Struts1 the common all the Action classes extend the base Action class which is an Abstract class. In contrast Struts 2 deals with Action Interfaces. There is no real need to implement an Action Interface. We generally implement ActionSupport class. The Action classes are not container dependent and the Action classes can be simple POJO's with execute signature.

2. Threading Model:- In Struts1, Action resources must be thread-safe or synchronized. So Actions are singletons and thread-safe, there should only be one instance of a class to handle all requests for that Action. The singleton strategy places restrictions on what can be done with Struts1 Actions and requires extra care to develop. However in case of Struts 2, Action objects are instantiated for each request, so there are no thread-safety issues. (In practice, servlet containers generate many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection.)

3. Control Of Action Execution:- Each module in Struts 1 has a separate Request Processors (lifecycles), while all the Actions in the module must share the same lifecycle.In Struts 2 different lifecycles are created on a per Action basis via Interceptor Stacks. Custom stacks are created and used with different Actions, as required.

4. Testability:- Testing Struts1 applications are a bit complex. A major hurdle to test Struts1 Actions is that the execute method because it exposes the Servlet API. A third-party extension, Struts TestCase, offers a set of mock object for Struts1. But the Struts 2 Actions can be tested by instantiating the Action, setting properties and invoking methods. Dependency Injection support also makes testing simpler. Actions in struts2 are simple POJOs and are framework independent,  hence testability is quite easy in Struts2.

5. Type Conversion:- for converting the Strings in ActionForm to actual types we need to rely on BeanUtils in Struts1. When it comes to Struts the Type conversion is done based on OGNL (Object Graph Notational Language).The framework includes converter for basic and common object types and primitives.


6. Binding values into views:- In the view section, Struts1 uses the standard JSP mechanism to bind objects (processed from the model section) into the page context to access. However Struts 2 uses a "ValueStack" technology so that the taglibs can access values without coupling your view to the object type it is rendering. The ValueStack strategy allows the reuse of views across a range of types which may have the same property name but different property types.

7. Expression Language:- Struts1 integrates with JSTL, so it uses the JSTL-EL. The struts1 EL has basic object graph traversal, but relatively weak collection and indexed property support. Struts 2 can also use JSTL, however it supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).


8. Validation:-  Struts1 uses the validate method of the ActionForm or Commons Validation to validate the properties. In contrast Struts 2 uses the XWork Validation framework. This provides the support to chaining of validation into sub-properties using the validations defined for the properties class type and the validation context.

9. Harvest Input:-  Struts1 uses an ActionForm object to capture input. And all ActionForms needs to extend a framework dependent base class. JavaBeans cannot be used as ActionForms, so the developers have to create redundant classes to capture input. However Struts 2 uses Action properties (as input properties independent of underlying framework) that eliminates the need for a second input object, hence reduces redundancy. Additionally in struts2, Action properties can be accessed from the web page via the taglibs. Struts 2 also supports the ActionForm pattern, as well as POJO form objects and POJO Actions. Even rich object types, including business or domain objects, can be used as input/output objects.








 

No comments:

Post a Comment