Architecture of Struts 2 Framework
In this chapter we will discuss about the Architecture and Lifecycle of Struts 2. This Struts 2 Architecture is based on WebWork and XWork framework. Struts 2 promotes the MVC framework and is tightly coupled framework. After reading this tutorial you will be able to understand the Architecture of Struts 2 framework.
Struts 2 framework is based on the concepts of MVC framework. The MVC design patterns divides the applications into three kinds of components namely Model, View and Controller. These components have specific roles and responsibilities.
Struts 2 framework is Open source popular MVC framework for writing the web based applications. It consist of Controller, Model and View components. Here are the description of each components of Struts 2 framework.
Controller:
Controller receives User input and validate it. Controller is responsible to perform the business operation that modifies the state of the data model. Controller has the ability to send the command to change View presentation of the model. It can also send command to update the Model state.
Model:
Model handles the data of the application and responds to the request from View and instructions from Controller. It then update itself accordingly and informs about the change in its state to associated Views and Controllers. Views than updates its output and Controllers change the available set of commands accordingly.
View:
View ask for information from Model via Controller that it requires to generate output for user. View is responsible for generating the UI interface for the application. It generates the view based on the model data.
Fig. MVC Application
Lifecycle of Struts 2:
- Client sends the Request in Struts 2 Framework.
- Request invokes the Servlet Container.
- It is then passed through standard filter chain
- FilterDispatcher filter is called.
- FilterDispatcher refers ActionMapper to decide whether an Action should be invoked or not.
- If Action is to be invoked, FilterDispatcher assigns control to ActionProxy
- ActionProxy reads the configuration file and creates an instance of ActionInvocation class.
- ActionInvocation invokes the Interceptors before calling the Action. (Note: Interceptors are only invoked if they are required)
- Once the Action returns, ActionInvocation looks for result associated with the Action result code.
- After the Action, Interceptors are executed again in reverse order and the response is returned to the Filter.
- Result is then sent to the Servlet Container, which displays it to the client.
Request processing lifecycle of Struts2 framework:
- User generated Request is sent to Servlet container.
- Servlet container invokes FilterDispatcher filter
- FilterDispatcher filter decides which action to call
- Intercetors are invoked (if any) before calling the Action.
- Action is executed
- Result is generated by Action.
- Interceptors are called again (if any)
- Output of Action is rendered in the View
- Result is displayed to the User