Friday, May 8, 2015

Struts 1 vs Struts 2

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. A servlet 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 page using 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 technology to 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 .

Interceptors in Struts2   Vs   RequestProcessor in Struts1



In Struts 1.x we have RequestProcessor class that provides most of request processing work. RequestProcessor class having many worker functions that does all of the work like finding ActionForm, getting Locale, finding Action class, calling validation etc.
Also custom RequestProcessor can be made to do extra processing work on request. On the same line, Struts 2 provides the Interceptor concept that is capable of doing all the request processing work. Struts 2 having default Interceptors stack that is responsible for doing many request processing work and defined in struts-default.xml and hence can be taken as RequestProcessor..



Struts 2 :  Interceptors
Interceptors are the most powerful feature implemented in Struts 2 framework that sets it apart from other framework. Interceptors, as name suggest, intercept the request and provides some additional processing before and after yhe execution of Action and result. Common functionalities or Common Concerns [ input data validation , file upload , protection of double submit etc] for different Actions are implemented in Interceptor. Struts 2 framework provides some predefined interceptors ready to use with added functionality of creating custome interceptor. These interceptors are used in specified order to achive the required work of Action class.
Struts 2 framework invokes a particular Action when request is meant for that but before going to Action object, invocation is intercepted by some other object that provides some additional processing on request. Similarly when response is getting back, reverse process of interception of objects will be carried out. This object is calledInterceptor.
Every Interceptor is pluggable and required interceptor or interceptor stack can be configured per Action class basis. Separation of core functionality code in form of Interceptor makes action more lightweight as it reduces the Action class of referencing other objcets in it. Also Interceptors provides more control at controller layer and separate some commong logic that applies to multiple Action classes.
Working of Interceptor
  1. An interceptor class can be created by implementing com.opensymphony.xwork2.interceptor.Interceptor interface or extending by com.opensymphony.xwork2.interceptor.AbstractInterceptor class.
  2. Every Interceptor has a methos named as String intercept(ActionInvocation invocation ) that allows some processing before and some processing after the request by ActionInvocation.
  3. If no processing by ActionInvocation, just short circuit the flow and return proper String code.

3 comments:

  1. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Product prototyping during this lockdown period everyone can use it wisely.
    icp-cat training

    ReplyDelete
  2. Thanks for sharing this informative content , Great work
    To crack scrum master interview : Scrum Master Interview Questions

    ReplyDelete
  3. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in ICP CAT during this lockdown period everyone can use it wisely.
    ICP-CAT certification

    ReplyDelete

Java 9 and Java11 and Java17, Java 21 Features

 Java 9 and Java11 and Java17 features along with explanation and examples in realtime scenarios Here's a detailed breakdown of Java 9, ...