Resources » Advanced Java
What is STRUTS2 and Features and architecture of STRUTS2 ?
This article brings you many things about struts2 and its features and advantages.I am taking this opportunity to write about struts2 in our spider community JSC.This article explains the architecture and workflow in struts 2.In this article you will come to know the struts2 features and similarities , differences between struts 1.x and struts 2.
In my previous article about WebWork we discussed about webwork and its features.Now we are going to read about struts 2.
Web Applications and types of Web Applications
If an application is deployed in a server(Web Server) which is accessible to all the clients and the server is accepting the requests from various clients (typically through browsers) and serving with appropriate responses,that application is said to be a Web Application.This Web Application resides in a web container which is software installed in the web server .This Container takes the Requests transfered and after execution of application ,this component sends the appropriate response to the Browser.
Types of Web Applications
Presentation oriented applications
Static and dynamic web pages for display at client end and contains content only.
Service oriented applications
Provides services to the presentation applications and implements presentation as well as business logic also.
What is Struts 2
Struts 2 is a open source web application Framework (we have discussed in previous article ), which developed on MVC 2(Model-view-Controller) architecture for developing Web Applications.Struts 2 is developed based on WebWork2.It is a flexible frame work for developing JAVA based web applications.Struts2 provides a framework for creating web applications which,separates View ,Model and Control on differant layers.In struts 2 dynamic data can be made with the help of mark-up language library.There is a huge tag library which supports JSP,Velocity,JSTL and AJAX and many other technologies.
Struts2Request Processing Flow
As and when the request received from the browser / client ,it checks for the matching interceptors,Action classes, and result with the configuration.Now the Configuration passes the request through interceptors for any pre-processing.After this pre-processing ,Action class's new instance will be created and appropriate method in Action class will be executed.This is the method specified in the configuration file(struts.XML) along with Action.
Once the processing is completed the return result class from this method will be defined.Return class's new instance will be invoked.Again the response also passes through the interceptors to perform post-processing .Now the result is passed to the web component , which inturn will passes to client as shown in diagram.
MVC pattern in STRUTS 2
1.Inputs from clients
2.Change of State
Features of struts 2
Main intention behind developing Struts 2 is to streamline the process of complete development cycle,which includes building,deploying and maintaining the applications.
Easy to create new projects
Supports JSP,FreeMarker,and velocity tags .No need to learn the taglib API's .
Tag libraries facilitates less coding
Smart UI tools like check boxes provided for user input
Provides support for AJAX tags which inturn help to design standard interactive web applications like struts.
Can be integrated with SPRING framework
Post-processing results can be used for JFreeChart,file downloading e.t.c
Directly taking binary and string properties for form inputs using java beans on an Action Class.
Interfaces are eliminated as interfacing betwee components are provided by Controller only.Any class can be a Action class.
Easy to de-bug as line number is provided.
Allows extension and plugins to increase the capabilities.
Actions can be directly tested.
controller can be customized easily to process the request as per action.
Built-in De-bugging tools.
The diagram shown above explains the architecture of STRUTS 2 and it is fully based on MVC architecture.Components are categorized into groups as follows:
ServletFilter - ActionContestCleanUp,SiteMesh,FilterDispatcher
Core - Action Proxy,ActionMapper,Tagsystem,Configuration Manger,ActionInvocation.
Interceptors - for pre-processing,post-processing.
User created components -Action classes,Templates,struts.xml.
In Struts 2 architecture ,when a request from the client arrived at web container ,it passes through all the three components of filters.
First, Request(HttpServletRequest) goes through ACC(ActionContextCleanup) filter , which works with FD(FilterDispatcher) and integrates with SiteMesh.The Actionmapper will be invoked by FD to find Action.FD also handle the execution process and cleans up AC(ActionContext).FD serves static content also.
The ActionMapper binds the HttpRequests and action requests and vice-versa.ActionProxy is called by ActionMapper to invoke an action.Now,the Action proxy invoke execution method in action class.Action proxy determines how the action should be handled from struts.xml.
Once the action is completed, the result is generated with the help of result component.The result template will be determined by result component with the help of struts.xml.After result template data finished ,Action Invocation produces the result using Interceptors.
Similarities between Struts1 and Struts 2
Both follow MVC Arhitecture
Both Use request handler to map classes with URIs.
Response handler is same.
Both uses tag library to create rich,responsive,form-based apps and generate dynamic pages<.li>
Differences between Struts1 and Struts2
In Struts1 Action class extends the abstract base class, where as in struts2 it implements com.opensymphony.xwork2.Action interface.
Binding Values into views
In Struts 1 , uses the standard JSP procedure for binding JSP objects into page context.
In Struts2 , value stack is used which helps taglibs to access vaalues without JSP view.
Servlet Dependacy : In struts1 with execute method request,response are pased to invoke an action.It is eliminated in struts2 actions are not coupled with container.(struts.xml)
Action forms are used in struts1 to get input from form.In struts2 this action properties can access from web page via taglibs.
In struts1 Validation is through validate method only.In Struts2 validation is through XWork validation framework.
Type conversion in struts2 is performed by ONGL, where as in struts1 by beanutils.
Thank you friends.
In next article we will discss about creating and deploying a STRUTS application
Did you like this resource? Share it with your friends and show your love!
No responses found. Be the first to respond...
Active MembersTodayLast 7 Daysmore...