Struts 2 Architecture and Flow

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.

  1. Struts 2 basic flow
  2. Struts 2 standard flow (Struts 2 architecture)
  3. How Struts Works
  4. Struts 2 Request Life Cycle
Struts 2 Basic Flow

Java Struts 2 Basic Flow

From the above diagram let us discuss the flow of Struts 2. Mentioned below are steps —

  1. A request is sent for the action from the user.
  2. ActionInvocation is invoked by the controller
  3. ActionInvocation invokes each interceptor and action
  4. Result generation has occurred
  5. The generated result is sent back to the ActionInvocation
  6. A HttpServletResponse is generated
  7. 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 

  1. Actions
  2. Interceptors
  3. Value Stack / OGNL
  4. Results / Result types
  5. View technologies

Java Struts 2 Architecture and Flow

From the above diagram, it can be seen that –

  1. The controller is implemented using a Struts 2 dispatcher servlet filter and interceptors.
  2. The model is implemented using actions.
  3. The view is implemented using result types and results.
  4. 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

  1. Container checks the request in the web.xml file and gets the class name of the controller.
  2. Container forms the controller (StrutsPrepareAndExecuteFilter or FilterDispatcher). Since struts2.1, it is StrutsPrepareAndExecuteFilter. Before 2.1 it was FilterDispatcher.
  3. The information gets by the controller for the action from the ActionMapper
  4. Controller form the ActionProxy
  5. ActionProxy gets the information of the action and interceptor stack from the configuration manager that gets information from the struts.xml file.
  6. ActionProxy is than forwards the request to the ActionInvocation
  7. ActionInvocation invokes each interceptor and action
  8. A result is generated
  9. The result is then sent back to the ActionInvocation
  10. A HttpServletResponse is than generated
  11. 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.

How do Struts work?

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
  1. User Sends request: User calls a request for the unspecified resource to the Servlet Container.
  2. FilterDispatcher determines the appropriate action: The FilterDispatcher accepts the request and checks the exact action corresponding to the request.
  3. Interceptors are applied: Interceptors accomplish the tasks i.e. workflow, validation, file upload, and automatically applied to the request.

Struts 2 Request Life Cycle

  1. 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.
  2. Output rendering: The Result is generated and is rendered in the view.
  3. Return of Request: The request returns through the interceptors in reverse order. It gives permission to perform the clean-up or additional processing.
  4. 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.

Leave a Reply

Your email address will not be published.