Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Struts 2 Architecture and flow
2.1.
Struts 2 basic flow
2.2.
Struts 2 standard flow (Struts 2 architecture)
3.
Frequently Asked Questions
3.1.
What are the Struts 2 core components?
3.2.
What is the interceptor in Struts2?
3.3.
Which design pattern is implemented by Struts2 interceptors?
3.4.
What is ValueStack?
3.5.
What is the use of a namespace in action mapping in Struts2?
4.
Conclusion
Last Updated: Mar 27, 2024

Struts 2 architecture

Introduction

A Struts 2 application’s architecture and flow depend on components like ActionMapper, Configuration Manager, Interceptor, Controller, etc.

Struts og image

In the following article, we will learn about the Struts 2 architecture in detail.

Struts 2 Architecture and flow

Here, we are going to understand the struts flow in two ways:

  1. struts 2 basic flow
  2. struts 2 standard architecture and flow provided by apache struts

Struts 2 basic flow

Let's understand the basic flow of struts 2 application using the figure given below-

Struts 2 basic flow
  1. The user submits an action request.
  2. The Controller triggers ActionInvocation.
  3. ActionInvocation calls each interceptor and action.
  4. There is a result.
  5. The ActionInvocation receives the outcome as a response.
  6. It produces a HttpServletResponse.
  7. An answer is delivered to the user.

Struts 2 standard flow (Struts 2 architecture)

Let's understand the basic architecture of the struts 2 application using the figure given below-

Struts 2 flow and architecture image
  1. The user submits an action request.
     
  2. The container translates the request in the web.xml file and obtains the Controller's class name.
     
  3. The Controller is called by the container (StrutsPrepareAndExecuteFilter or FilterDispatcher). It is StrutsPrepareAndExecuteFilter as of struts 2.1. It was FilterDispatcher prior to 2.1.
     
  4. ActionMapper provides the information to the Controller for the action.
     
  5. The Controller calls ActionProxy.
     
  6. From the configuration manager, which receives its data from the struts.xml file, ActionProxy obtains information about the action and interceptor stack.
     
  7. The request is forwarded to the ActionInvocation by ActionProxy.
     
  8. ActionInvocation launches each interceptor and action.
     
  9. There is a result.
     
  10. The ActionInvocation receives the outcome as a response.
     
  11. It produces a HttpServletResponse.

Frequently Asked Questions

What are the Struts 2 core components?

You don't use Spring MVC and Struts together because they are interchangeable. However, showing activity on this post is acceptable when Struts and Spring IoC are combined. MVC framework is typically provided by Struts (most Production support & maintenance applications are already integrated with it).

What is the interceptor in Struts2?

Struts 1 allows for distinct Request Processors (lifecycles) for every module, but every module's Actions must use the same lifecycle. Through interceptor stacks, Struts 2 creates various lifecycles for each action. As required, custom stacks can be constructed and applied to multiple Actions.

Which design pattern is implemented by Struts2 interceptors?

The design pattern for intercepting filters is the foundation for Struts2 interceptors. The Chain of Responsibility design pattern is quite similar to how interceptors are invoked in an interceptor stack.

What is ValueStack?

ValueStack is the storage location where Struts2 stores the application data needed to process client requests. ActionContext objects that use ThreadLocal to have values particular to the particular request thread store the data.

What is the use of a namespace in action mapping in Struts2?

The namespace setup in Struts2 makes it simple to develop modules. Our action classes can be divided using namespaces according to how they should be used, such as admin, user, customer, etc.

Conclusion

This article taught us about the architecture of Struts 2. We began with a brief introduction to architecture and then dived deep into its types. 

Recommended Reading: 

Spring Boot Architecture

After reading about Struts 2 architecture, refer to Struts 2 documentationStruts Tutorial for beginnersStruts 2 Intro, and 15 Best Java frameworks to use in 2022-Coding Ninjas for a deeper understanding of Struts 2 development and other related topics.

Thank you image
Live masterclass