Thursday, September 27, 2007

Struts2 - Part I : An Introduction

About
Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts 2.


Platform Requirements
Servlet API 2.4
JSP API 2.0
Java 5
An alternate set of JARs for Java 4 are also available.


Struts 2 Vs Struts 1.x
Top 10 distinctions :
1. Servlet Dependency:
Struts 1 : Actions have dependencies on the servlet API since the HttpServletRequest and HttpServletResponse objects are passed to the execute method when an Action is invoked but Struts 2 : Actions are not container dependent because they are made simple POJOs.
Actions can access the original request and response, if required. However, other architectural elements reduce or eliminate the need to access the HttpServetRequest or HttpServletResponse directly.
2. Action classes
Programming the abstract classes instead of interfaces is one of design issues of struts1 framework that has been resolved in the struts 2 framework.
Struts 1 : Action classes needs to extend framework dependent abstract base class.
Struts 2 : Action class may or may not implement interfaces to enable optional and custom services. Struts 2 provides a base ActionSupport class to implement commonly used interfaces. Albeit, the Action interface is not required. Any POJO object with a execute signature can be used as an Struts 2 Action object.
3. Validation
Struts 1 and Struts 2 both supports the manual validation via a validate method.
Struts 1: Uses validate method on the ActionForm, or validates through an extension to the Commons Validator.
Struts 2: Supports manual validation via the validate method and the XWork Validation framework. The Xwork Validation Framework supports chaining validation into sub-properties using the validations defined for the properties class type and the validation context.
4. Threading Model
Struts 1: 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 Struts 1 Actions and requires extra care to develop.
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.)
5. Testability
Struts 1: Testing applications are a bit complex. A major hurdle to test Struts 1 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 Struts 1.
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.
6. Harvesting Input
Struts 1: 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.
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.
7. Expression Language
Struts 1: 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: Also uses JSTL, however it supports a more powerful and flexible expression language called "Object Graph Notation Language" (OGNL).
8. Binding values into views
Struts 1: In the view section, uses the standard JSP mechanism to bind objects(processed from the model section) into the page context to access .
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.
9. Type Conversion
Struts 1: ActionForm properties are all Strings. Struts 1 uses Commons-Beanutils for type conversion. These type converters are per-class, and not configurable per instance.
Struts 2: uses OGNL for type conversion. The framework includes converters for basic and common object types and primitives.
10. Control Of Action Execution
Struts 1: Supports separate Request Processors (lifecycles) for each module, but all the Actions in a module must share the same lifecycle.
Struts 2: Supports creating different lifecycles on a per Action basis via Interceptor Stacks. Custom stacks can be created and used with different Actions, as needed.








No comments:

Post a Comment