Back to: Java Struts Tutorials
Java Struts 2 Architecture and Flow
In this article, I am going to discuss Java Struts 2 Architectures and Flow in detail. Please read our previous article where we discussed Struts 2 Components. At the end of this article, you will understand the following pointers in detail.
- Struts 2 basic flow
- Struts 2 standard flow (Struts 2 architecture)
- How Struts Works
- Struts 2 Request Life Cycle
Struts 2 Basic Flow
From the above diagram let us discuss the flow of Struts 2. Mentioned below are steps —
- A request is sent for the action from the user.
- ActionInvocation is invoked by the controller
- ActionInvocation invokes each interceptor and action
- Result generation has occurred
- The generated result is sent back to the ActionInvocation
- A HttpServletResponse is generated
- The generated response is sent to the user
Struts 2 Standard Flow (Struts 2 Architecture)
From a top-level view, basically Struts 2 is an MVC2 framework. The five core components of the Model-View-Controller pattern in Struts 2 are
- Actions
- Interceptors
- Value Stack / OGNL
- Results / Result types
- View technologies
From the above diagram, it can be seen that –
- The controller is implemented using a Struts 2 dispatcher servlet filter and interceptors.
- The model is implemented using actions.
- The view is implemented using result types and results.
- The value stack and OGNL can be used to provide common thread, linking, and enabling integration among the other components.
The flow of the Struts 2 Architecture:
Now let us discuss the flow of the Struts 2 Architecture
- Container checks the request in the web.xml file and gets the class name of the controller.
- Container forms the controller (StrutsPrepareAndExecuteFilter or FilterDispatcher). Since struts2.1, it is StrutsPrepareAndExecuteFilter. Before 2.1 it was FilterDispatcher.
- The information gets by the controller for the action from the ActionMapper
- Controller form the ActionProxy
- ActionProxy gets the information of the action and interceptor stack from the configuration manager that gets information from the struts.xml file.
- ActionProxy is than forwards the request to the ActionInvocation
- ActionInvocation invokes each interceptor and action
- A result is generated
- The result is then sent back to the ActionInvocation
- A HttpServletResponse is than generated
- The response is then sent to the user.
How do Struts work?
web.xml file contains all the configuration information related to the web application. It set up the environment to start the application. In the web.xml file, StrutsPrepareAndExecuteFilter is configured to handle the request from the client and delegate with the Struts environment.
The StrutsPrepareAndExecuteFilter prepares and executes different phases of the Struts dispatching process. We recommend to use it if in case you don’t have any other filter, which needs access to action context information.
Request: This is the very first step, using a web browser the client makes a request for an individual resource, which is then sent by the web container. After that, the web container loads web.xml and confirms whether the URL patterns matched or not. Once the verification is verified, the web container transfers the request to Filter Dispatcher.
StrutsPrepareAndExecuteFilter: Once the request is sent to the filter dispatcher estimate the request and checks an appropriate action as per the mapping of the URL (ActionMapper) calls the ActionProxy then reads the configuration file manager (like the struts.xml file) to check an exact action for the request. ActionProxy which reads then creates an ActionInvocation, accounts for the execution of command pattern implementation then the request is sent to the proper Action Class.
Interceptor Stacks: Before receiving the Action Class, the request passes through the Interceptor Stacks, where the list of interceptors is identified which are essential to be checked before creating the Action class.
Action Class: then the request that passes through the Action class, which then accomplishes the code and finally generates the result of execution as Success or Input or Error.
Result: Depending on the code that is being resulted, the Controller finds View to be and hand over the result of Action. At the time of handling, Struts tags furnished by the framework and can be used by the templates.
Interceptors Stack: Before calling the client, the interceptors are to be checked again and the response returns to the user through the filters that are being configured in the web.xml.
Struts 2 Request Life Cycle
- User Sends request: User calls a request for the unspecified resource to the Servlet Container.
- FilterDispatcher determines the appropriate action: The FilterDispatcher accepts the request and checks the exact action corresponding to the request.
- Interceptors are applied: Interceptors accomplish the tasks i.e. workflow, validation, file upload, and automatically applied to the request.
- Execution of Action: Then the action method is responsible to perform the database related operations such as retrieving data or storing data in a database.
- Output rendering: The Result is generated and is rendered in the view.
- Return of Request: The request returns through the interceptors in reverse order. It gives permission to perform the clean-up or additional processing.
- Display the result to the user: The control is returned to the servlet container which sends the output to the user browser.
In the next article, I am going to discuss Java Struts 2 Environment Setup. Here, in this article, I try to explain Java Struts 2 Architectures and Flow and I hope you enjoy this Java Struts 2 Architectures and Flow article.