Struts2 Basic

Struts

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.

Model: Action
View: JSP / Velocity Template / Freemaker
Controller: StrutsPrepareandExecuteFilter

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

Diagramme::::::

Request Life Cycle in Struts2 Framework:

  • 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.

By | 2012-04-28T05:21:02+00:00 April 28th, 2012|Uncategorized|4 Comments

About the Author:

4 Comments

  1. Pranav December 11, 2012 at 6:31 am - Reply

    Article is beneficial for beginners,to get confidence.Though if Diagrammatically life cycle would be described it would be nice.

    • admin December 11, 2012 at 6:58 am - Reply

      Hi pranav,
      Thanks for your suggestions we’ll try to provide life cycle in graphical format asap.

  2. Sandeep Reddy April 3, 2013 at 12:46 pm - Reply

    hi admin,

    plz try to provide examples for struts to understand basics

  3. b.vinoth April 4, 2013 at 5:10 am - Reply

    So nice explanation , thanks a lot

Leave A Comment