Apache Struts2 framework is a combined work of webwork and Struts community. Previously it was known as webwork2 which later on named as Struts2. It uses Inversion Of Control (IoC) and architectural pattern MVC(Model-View-Controller), It also enables Aspect-Oriented Programming (AOP) using Interceptors. As Struts2 has built in support of AJAX, it is also ideally suitable for developing web 2.0 application.
View: JSP / Velocity Template / Freemaker
The controller’s job is to map the user request to appropriate action. Model contains the data and the business logic. In Struts 2 the model is implemented by the Action component. View is the presentation component of the MVC Pattern. In Struts 2 the view is commonly implemented using JSP, Velocity Template, Freemaker or some other presentation-layer technology.
Struts2 is a pull-MVC framework. i.e. the data that is to be displayed to user has to be pulled from the Action.
Struts2 Architectural: It leverages the standard JEE technologies such as Java Filters, JavaBeans, ResourceBundles, Locales, XML etc in its architecture. Following is framework diagram.
Image Courtesy : struts.apache.org
- Lifecycle begins with the request sent from User. Once this request reaches the ServetContainer container passes the request to Filter according to configuration defined in web.xml. If request need to forwarded to Struts2 Framework request will go to Filter(StrutsPrepareAndExecuteFilter / StrutsPrepareFilter / StrutsExecuteFilter).
- Filter then called which consults the ActionMapper to determine whether an Action should be invoked or not.
- If ActionMapper finds an Action to be invoked, the FilterDispatcher delegates control to ActionProxy.
- ActionProxy reads the configuration file such as struts.xml. ActionProxy creates an instance of ActionInvocation class and delegates the control.
- ActionInvocation is responsible for command pattern implementation. It invokes the Interceptors one by one (if required) and then invoke the Action.
- 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 JSP or FreeMarker to be rendered. While rendering, 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.
- The Interceptors are executed again in reverse order and the response is returned to the Filter. And the result is then sent to the servlet container which in turns sends it back to client.
- If the ActionContextCleanUp filter is present, the FilterDispatcher will not clean up the ThreadLocal ActionContext. If the ActionContextCleanUp filter is not present, the FilterDispatcher will cleanup all ThreadLocals.
Note : 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.