Thursday, 20 October 2011

Feature of Struts 2


The new version Struts 2.0 is a combination of the Sturts action framework and Webwork. According to the Struts 2.0.1 release announcement, some key features are:
  • Simplified Design - Programming the abstract classes instead of interfaces is one of design problem of struts1 framework that has been resolved in the struts 2 framework. Most of the Struts 2 classes are based on interfaces and most of its core interfaces are HTTP independent. Struts 2 Action classes are framework independent and are simplified to look as simple POJOs. Framework components are tried to keep loosely coupled.
  • Simplified Actions  - Actions are simple POJOs. Any java class with execute() method can be used as an Action class. Even we don't need to implement interfaces always. Inversion of Control is introduced while developing the action classes. This make the actions to be neutral to the underlying framework .
  • No more ActionForms  - ActionForms feature is no more known to the struts2 framework. Simple JavaBean flavored actions are used to put properties directly. No need to use all String properties.
  • Simplified testability - Struts 2 Actions are HTTP independent and framework neutral. This enables to test struts applications very easily without resorting to mock objects.
  • Intelligent Defaults - Most configuration elements have a default value which can be set according to the need. Even there are xml-based default configuration files that can be overridden according to the need.
  • Improved  results - Unlike ActionForwards, Struts 2 Results provide flexibility to create multiple type of outputs and in actual it helps to prepare the response.
  • Better Tag features - Struts 2 tags enables to add style sheet-driven markup capabilities, so that we can create consistent pages with less code. Struts 2 tags are more capable and result oriented. Struts 2 tag markup can be altered by changing an underlying stylesheet. Individual tag markup can be changed by editing a FreeMarker template. Both JSP and FreeMarker tags are fully supported.
  • Annotations introduced : Applications in struts 2 can use Java 5 annotations as an alternative to XML and Java properties configuration. Annotations minimize the use of xml.
  • Stateful Checkboxes - Struts 2 checkboxes do not require special handling for false values.
  • QuickStart - Many changes can be made on the fly without restarting a web container.
  • customizing controller - Struts 1 lets to customize the request processor per module, Struts 2 lets to customize the request handling per action, if desired.
  • Easy Spring integration - Struts 2 Actions are Spring-aware. Just need to add Spring beans!
  • Easy plugins - Struts 2 extensions can be added by dropping in a JAR. No manual configuration is required!
  • AJAX support - The AJAX theme gives interactive applications a significant boost.
    The framework provides a set of tags to help you ajaxify your applications, even on Dojo. The AJAX features include:
    1. AJAX Client Side Validation
    2. Remote form submission support (works with the submit tag as well)
    3. An advanced div template that provides dynamic reloading of partial HTML
    4. An advanced template that provides the ability to load and evaluate JavaScript remotely
    5. An AJAX-only tabbed Panel implementation
    6. A rich pub-sub event model
    7. Interactive auto complete tag

Tuesday, 11 October 2011

Struts1 vs. Struts2


FeatureStruts 1Struts 2
Action classesStruts1 extends the abstract base class by its action class. The problem with struts1 is that it uses the abstract classes rather than interfaces.While in Struts 2, an Action class implements an Action interface, along with other interfaces use optional and custom services. Struts 2 provides a base ActionSupport class that implements commonly used interfaces. Although an Action interface is not necessary, any POJO object along with an execute signature can be used as an Struts 2 Action object.
Threading ModelStruts 1 Actions are singletons therefore they must be thread-safe because only one instance of a class handles all the requests for that Action. The singleton strategy restricts to Struts 1 Actions and requires extra care to make the action resources thread safe or synchronized while developing an application.Struts 2 doesn’t have thread-safety issues as Action objects are instantiated for each request. Aservlet container generates many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection.
Servlet DependencyActions are dependent on the servlet API because HttpServletRequest and HttpServletResponse is passed to the execute method when an Action is invoked therefore Struts1.Container does not treat the Struts 2 Actions as a couple. Servlet contexts are typically represented as simple Maps that allow Actions to be tested in isolation. Struts 2 Actions can still access the original request and response, if required. While other architectural elements directly reduce or eliminate the need to access the HttpServetRequest or HttpServletResponse.
TestabilityStruts1 application has a major problem while testing the application because the execute method exposes the Servlet API. Struts TestCase provides a set of mock object for Struts 1.To test the Struts 2 Actions instantiate the Action, set the properties, and invoking methods. Dependency Injection also makes testing easier.
Harvesting InputStruts 1 recieves an input by creating an ActionForm object. Like the action classes, all ActionForms class must extend a ActionForm base class. Other JavaBeans classes cannot be used as ActionForms, while developers create redundant classes to receive the input. DynaBeans is the best alternative to create the conventional ActionForm classes.Struts 2 requires Action properties as input properties that eliminates the need of a second input object. These Input properties may be rich object types, since they may have their own properties. Developer can access the Action properties from the web pageusing the taglibs. Struts 2 also supports the ActionForm pattern, POJO form objects and POJO Actions as well.
Expression LanguageStruts1 integrates with JSTL, so it uses the JSTL EL. The EL has basic object graph traversal, but relatively weak collection and indexed property support.Struts 2 can use JSTL, but the framework also supports a more powerful and flexible expression language called “Object Graph Notation Language” (OGNL).
Binding values into viewsStruts 1 binds objects into the page context by using the standard JSP mechanism.Struts 2 uses a ValueStack technologyto make the values accessible to the taglibs without coupling the view to the object to which it is rendering. The ValueStack strategy enables us to reuse views across a range of types, having same property name but different property types.
Type ConversionStruts 1 ActionForm properties are almost in the form of Strings. Commons-Beanutils are used by used by Struts 1 for type conversion. Converters are per-class, which are not configurable per instance.Struts 2 uses OGNL for type conversion and converters to convert Basic and common object types and primitives as well.
ValidationStruts 1 uses manual validation that is done via a validate method on the ActionForm, or by using an extension to the Commons Validator. Classes can have different validation contexts for the same class, while chaining to validations on sub-objects is not allowed.Struts 2 allows manual validation that is done by using the validate method and the XWork Validation framework. The Xwork Validation Framework allows chaining of validations into sub-properties using the validations defined for the properties class type and the validation context.
Control Of Action ExecutionEach 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.s