How Struts 2 Framework works?
This tutorial explains you the working of the Struts 18.104.22.168 framework. Struts 2 framework implements MVC architecture, which divides the application components into Model, View and Controller parts. In this tutorial you will learn How Struts 2 works with the help of an easy and simplified Struts Execution Flow Diagram.
Struts 2 - Open source MVC Framework
Struts 2 is the Open source web application Framework to create enterprise-ready Java web applications and has been designed in such a way that has made creation of web applications in Java quite simple and easy. Struts 2 is based on Model View Framework (MVC) architect, which has been developed using WebWork and Xwork.
Here are the details of each components of Struts 2 MVC framework.
Controller maps the user request to specific action. In Struts 2 StrutsPrepareAndExecuteFilter acts as Controller. Controller receives the user request and decides which action to invoke.
Struts 2 framework creates an instance of this action, invoke interceptors (if any). Then it calls invoke() method of ActionInvocations that executes the action. ActionInvocation then calls the intercept() method. The intercept() method of ActionInvocation class in turn calls the invoke() method of the ActionInvocation till all the interceptors are invoked. Interceptor are invoked before and after the action is executed. Interceptors are executed in the order they are defined in the stack. Finally action itself is invoked and the result is generated. All the interceptors are then invoked again but this time in the reverse order.
Model contains the data and the business logic and is implemented by the Action component.
View displays the result. View can be either JSP page, Velocity templates, XSLT pages, Freemaker or some other presentation-layer technology. Object-Graph Navigation Language (OGNL) is used to reference and manipulate data on the ValueStack.
How Struts 2 framework works?
Container reads the WEB-INF/web.xml file, which has all configuration information about out web application. It reads the information from the web.xml file and configures the Struts 2 environment on the startup of the application. In the web.xml file StrutsPrepareAndExecuteFilter is configured to handle the request from client and delegate with the Struts environment.
The StrutsPrepareAndExecuteFilter is responsible for preparing as well as executing the various phases of the Struts dispatching process. However, it is recommended to use it, if you don't have other filter, which needs access to action context information, like Sitemesh.
The following diagram shows the working of Struts 2 framework:
Here is the detailed description of how Struts 2 framework works from the time user clicks a URL to the time result is displayed.
Request: In the first step the client makes a request for a specific resource using a web browser, which is then received by the web container. After that, the web container loads web.xml and verifies whether the URL patterns matches or not. Once the verification is ok, the web container transfers the request to Filter Dispatcher.
StrutsPrepareAndExecuteFilter: Once the request is transferred to the filter dispatcher evaluates the request and decides an appropriate action as per the mapping of URL (ActionMapper), which then calls the ActionProxy, which then reads the configuration file manager (like the struts.xml file) and then determines an appropriate action for the request. ActionProxy then creates an ActionInvocation, which accounts for the implementation of command pattern implementation and the request is forwarded to appropriate Action Class.
Interceptor Stacks: However before calling to the Action Class, the request is passed to Interceptor Stacks, where the list of interceptors are identified that are required to be processed before invoking to the Action class.
Action Class: After that, the request is sent to the Action class, which then executes the codes, which finally generates the result of execution as Success or Input or Error.
Result: Depending on the result code, Controller then identifies View to be rendered as a result of Action. However, at the time of rendering, Struts tags provided by the framework can be used by the templates.
Interceptors Stack: Now, before responding back to the client, the interceptors are executed again and the response returns to the user through the filters configured in the web.xml.