CSS Drop Down Menu

Monday, 20 January 2014

Struts2.x:Features And MVC Architecture

Struts 2 Framework:-

The struts 2 framework is used to develop MVC-based web application.Apache Struts is a free, open-source, MVC framework for creating elegant, modern Java web applications. The Struts 2 framework is used to develop MVC (Model View Controller) based web applications. Apache Struts 2 was originally known as WebWork 2. After working independently for several years, the WebWork and Struts communities joined forces to create Struts2. This new version of Struts is simpler to use and closer to how Struts was always meant to be.In Another word we can say that Struts 2 is the combination of webwork framework of opensymphony and struts1. struts2 = webwork + struts1 The Struts 2 provides supports to POJO based actions, Validation Support, AJAX Support, Integration support to various frameworks such as Hibernate, Spring, Tiles etc, support to various result types such as Freemarker, Velocity, JSP etc.
Note :-
OpenSymphony was an Open Source computer software project dedicated to providing enterprise class Java EE components. OpenSymphony shut down sometime around 2009-2011, but some of the subprojects are now maintained elsewhere. Patrick Lightbody was one of the main programmers at the company.
WebWork and XWork : Core Engine Of OpenSymphony

Struts 2 Features:-

Struts 2 provides many features that were not in struts 1. The important features of struts 2 framework are as follows:
[1]:-Configurable MVC components
In struts 2 framework, we provide all the components (view components and action) information in struts.xml file. If we need to change any information, we can simply change it in the xml file.
[2]:-POJO based actions
In struts 2, action class is POJO (Plain Old Java Object) i.e. a simple java class.
[3]:-AJAX support
Struts 2 provides support to ajax technology.
[3]:-Integration support
We can simply integrate the struts 2 application with hibernate, spring, tiles etc. frameworks.
[4]:-Various Result Types
We can use JSP, freemarker, velocity etc. technologies as the result in struts 2.
[5]:-Various Tag support
Struts 2 provides various types of tags such as UI tags, Data tags, control tags etc to ease the developement of struts 2 application.
[6]:-Theme and Template support
Struts 2 provides three types of theme support: xhtml, simple and css_xhtml. The xhtml is default theme of struts 2. Themes and templates can be used for common look and feel.

WorkFlow in Struts-2.x:-

Image Courtesy: struts.apache.org [1]-The normal lifecycle of struts begins when the request is sent from client. This results invoke the servlet container which in turn is passed through standard filter chain. [2]-The FilterDispatcher filter is called which consults the ActionMapper to determine whether an Action should be invoked. [3]-If ActionMapper finds an Action to be invoked, the FilterDispatcher delegates control to ActionProxy. [4]-ActionProxy reads the configuration file such as struts.xml. ActionProxy creates an instance of ActionInvocation class and delegates the control. [5]-ActionInvocation is responsible for command pattern implementation. It invokes the Interceptors one by one (if required) and then invoke the Action. [6]-Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml. [7]-The Interceptors are executed again in reverse order and the response is returned to the Filter (In most cases to FilterDispatcher). And the result is then sent to the servlet container which in turns send it back to client.
Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml. The result is then executed, which often (but not always, as is the case for Action Chaining) involves a template written in JSPor FreeMarker to be rendered. While rendering, the templates can use the Struts Tags provided by the framework. Some of those components will work with the ActionMapper to render proper URLs for additional requests.
Note :-
Action Chaining:- The framework provides the ability to chain multiple actions into a defined sequence or workflow. This feature works by applying a Chain Result to a given Action, and intercepting its target Action's invocation with a ChainingInterceptor. FreeMarker:- FreeMarker is a Java-based template engine that is a great alternative to JSP. FreeMarker is ideal for situations where your action results can possibly be loaded from outside a Servlet container. For example, if you wished to support plugins in your application, you might wish to use FreeMarker so that the plugins could provide the entire action class and view in a single jar that is loaded from the classloader.
All objects in this architecture (Actions, Results, Interceptors, and so forth) are created by an ObjectFactory. This ObjectFactory is pluggable. We can provide our own ObjectFactory for any reason that requires knowing when objects in the framework are created. A popular ObjectFactory implementation uses Spring as provided by the Spring Plugin.

MVC Architecture:-

Model The model represents the state (data) and business logic of the application. View The view module is responsible to display data i.e. it represents the presentation. Controller The controller module acts as an interface between view and model. It intercepts all the requests i.e. receives input and commands to Model / View to change accordingly
Disadvantage MVC Architecture:-
We need to write the controller code self. If we change the controller code, we need to recompile the class and redeploy the application

1 comment: