November 28, 2012 by Nagesh Chauhan
Struts 2 is an open source framework given by Apache software foundation under one of its projects called Jakarta. It is the most commonly and widely used framework to develop web applications for java .
Initialy Struts 1 was introduced by Apache in 2004 , but later on to improve the performance and make it more flexible Apache joined with OpenSymphony and created struts2.x. Struts 2 is not an extension of struts 1, its the combination of Struts 1 and webwork2 some features taken from struts1 and some from webwork2 and finally released this struts2 framework.
Struts 2 is a Pull-MVC framework. The data that is to be displayed to user has to be pulled from the Action . Struts 2 supports both annotation and xml based mapping . Unlike struts 1 the action in Struts 2 are pure java classes(POJO). There is no need to implement any interface or extend any class for an action class. Use of Interceptors and view point is very much flexible as we can use jsp, freemarker and much more.
Features of Struts 2
(A) Any class can be used as an action class and one can input properties by using any JavaBean directly to the action class.
(C) Pure MVC based architecture that ensures good practices and reusability.
(D) A rich ‘Struts-Tags’ support like form tags , ui tags, data tags etc . It makes the designing and data handling simple and transparent.
(E) Customized request handling in each action makes it casy to customize when required.
(F) Spring friendly architecture and can be integrated to spring easily .Spring IOC can be used with a minimal code change.
Architecture of Struts 2 Application
The architecture of Struts2 applications can be better understand from the image given below.
Action ContextCleanUp filter The ActionContextCleanUp filter is optional and it is useful only when integration has to be done with other technologies or Plugin.
ActionMapper ActionMapper is an interface that provides a mapping between HTTP requests and action invocation requests and vice-versa. When a HttpServletRequest comes, the ActionMapper tried to match an appropriate action invocation request, it return null if no action invocation request matches , or it may return an ActionMapping that describes an action invocation for the framework to try.
FilterDispatcher FilterDispatcer uses the ActionMapper to determine weather to invoke an Action. If the action is required to be invoked, the FilterDispatcher delegates the control to the ActionProxy.
ActionProxy: The ActionProxy consult with Configuration Files manager, which is initialized from the struts.xml. Then the ActionProxy creates an ActionInvocation, which implements the command pattern.
ActionInvocation : ActionInvocation invokes the Interceptors (if configured) and then invokes the action. The ActionInvocation looks for proper result. Then the result is executed, which involves the rendering of jsp or templates.
Interceptors Interceptors are invoked both before and after the execution of the action and the results are rendered back to the user. Framework first finds which Action class to invoke for this request and discovers the interceptors associated with the action mapping. Now an instance of ActionInvocation is createrd and its invoke() method is called. ActionInvocation is the one which holds information about the action and the associated intercteptors. ActionInvocation knows in which sequence the interceptors should be invoked. ActionInvocation now invokes the intercept() method of the first interceptor in the stack snd so on. After the execution of all the interceptors the action class will be invoked. Finally a result string will be returned and the corresponding view will be rendered. There is much more to understand in Interceptors we w’ll discuss it later in details.
At the end the Interceptors are executed again in reverse order. Finally the response returns through the filters configured in web.xml . If the ActionContextCleanUp filter is configured, the FilterDispatcher does not clean the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not present then the FilterDispatcher will cleanup all the ThreadLocals.
Struts 2 Framework is based on MVC (Model-View-Controller) architecture. The logic behind the MVC architecture is to separate all the three parts the object model of the application, the view through which the user interacts with the application and the controller that controls the all the processing done by the application.
The Model is the part of the application that handles the logic for the application data.
Often model objects retrieve data (and store data) from a database. Struts 2 can be integrated with other framework like Hibernate to handle this part of MVC.
The View is the parts of the application that handles the presentation of the data.
Most often the views are created from the model data.
The controller controls the application interaction between the view and model. Typically controllers read data from a view, control user input, and send input data to the model.
The Flow of Struts 2 based Application
Lets now discuss the request lificycle of Struts 2
- User Sends request: The normal lifecycle of struts begins when the request is sent from client. This invokes the ServletContainer and the requests is passes through a chain of filters .
- FilterDispatcher determines the appropriate action: The FilterDispatcher looks at the request and consult to ActionMapper to determine the appropriate Action. If ActionMapper finds an Action to be invoked, then FilterDispatcher delegates control to ActionProxy. ActionProxy reads the configuration file ( struts.xml). ActionProxy creates an instance of ActionInvocation class and delegates the control.
- Interceptors are applied: ActionInvocation invokes the Interceptors one by one (if required) and then invoke the Action. Common functionalities such as workflow, validation, file upload etc. are automatically applied to the request.
- Action is executed : All the database interaction and insert, update delete activities are performed . Then the control returns to struts.xml again. Once the Action returns, the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped In struts.xml.
- Result is displayed : 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.The FilterDispatcher filter is called which consults the ActionMapper to determine whether an Action should be invoked.
Now we have an idea of basic structure and functioning of Struts2 , in next blog we w’ll start developing a Strts2 sample project from scratches.
Hope it helps !