Understanding how a web framework processes requests is fundamental to leveraging its capabilities effectively. Struts2, an MVC-based framework, orchestrates a sophisticated workflow to handle incoming HTTP requests, execute business logic, and render responses. This document explores the core components and the sequential steps involved in the Struts2 request processing lifecycle, using a practical example.
Illustrative Example: A Simple Struts2 Action
To grasp the underlying mechanism, let's consider a basic Struts2 setup. This example demonstrates how a specific URL triggers an action and renders a corresponding view.
1. Defining an Action Class
Actions in Struts2 encapsulate the logic for specific requests. They typically extend com.opensymphony.xwork2.ActionSupport, which provides default implementations for common methods like execute() and validation support. Below is a simple action that returns a predefined string:
package com.example.web.actions;
import com.opensymphony.xwork2.ActionSupport;
public class WelcomeAction extends ActionSupport {
private static final long serialVersionUID = 1L;
@Override
public String execute() {
// Here, business logic would typically be performed.
// For this example, we simply return a success string.
return "showWelcome";
}
}
2. Configuring the Struts2 Framework (struts.xml)
The struts.xml file is the central configuration point where actions are mapped to URLs and their corresponding results (views) are defined. The following snippet registers our WelcomeAction:
<?xml version="1.0" encoding="UTF-8"?>
<struts>
<package name="default" extends="struts-default">
<action name="displayWelcome" class="com.example.web.actions.WelcomeAction">
<result name="showWelcome">/WEB-INF/views/welcome.jsp</result>
</action>
</package>
</struts>
In this configuration:
- The
<action name="displayWelcome" ...>element maps the URL path/displayWelcometo ourWelcomeActionclass. - Inside the action,
<result name="showWelcome">specifies that if theexecute()method ofWelcomeActionreturns the string"showWelcome", the framework should dispatch to/WEB-INF/views/welcome.jsp.
3. Creating the View (welcome.jsp)
This JSP file serves as the view component, displaying the output to the user:
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h2>Hello from Struts2 Welcome Action!</h2>
</body>
</html>
When you access http://localhost:8080/yourAppName/displayWelcome in a browser, Struts2 processes the request based on the configurations, executes WelcomeAction, and renders welcome.jsp.
Struts2 Request Processing Workflow
The journey of an HTTP request through the Struts2 framework involves several key components, orchestrated to provide a robust and extensible processing pipeline:

(Note: An image depicting the flow diagram would be beneficial here, illustrating the sequence from Filter to Action to Result. Since no image was provided, this is a placeholder.)
1. Initial Request and Filter Interception
When a client sends an HTTP request (e.g., http://localhost:8080/yourAppName/displayWelcome), the web server (e.g., Tomcat) receives it and encapsulates it into an HttpServletRequest object.
Struts2's primary entry point is a Servlet Filter, typically configured in web.xml:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
The StrutsPrepareAndExecuteFilter (or FilterDispatcher in older versions) acts as the framework's front controller, intercepting all requests matching its url-pattern.
2. ActionMapper and Request Identification
Inside the filter, an ActionMapper determines if the incoming request URL maps to a configured Struts2 Action. It parses the URL to extract the action name (e.g., "displayWelcome" from /displayWelcome.action or /displayWelcome if suffix mapping is enabled).
3. ActionProxy Creation
If the ActionMapper identifies a matching action, the StrutsPrepareAndExecuteFilter delegates control to an ActionProxy. The ActionProxy is responsible for coordinating the execution of the action.
4. Configuration Lookup (ConfigurationManager)
The ActionProxy consults the framework's configuration (managed by the ConfigurationManager, which parses struts.xml and other configuration sources) to find the specific Action class associated with the identified action name (e.g., com.example.web.actions.WelcomeAction).
5. ActionInvocation Instantiation
An ActionInvocation instance is then created by the ActionProxy. The ActionInvocation represents a single execution of an action, including any associated interceptors.
6. Interceptor Chain and Action Execution
The ActionInvocation manages the execution flow. It first invokes any interceptors configured for the action. Interceptors are objects that can perform pre-processing (before the action method) and post-processing (after the action method but before the result is rendered). After all interceptors in the chain have executed their "before" logic, the target action's method (typically execute()) is invoked.
7. Result Mapping and View Rendering
Upon the completion of the action method, it returns a string (e.g., "showWelcome"). The ActionInvocation uses this return value to look up the corresponding <result> element in the struts.xml configuration. Once the result type (e.g., JSP dispatcher) and its location are identified, the framework forwards the request to the specified view (e.g., /WEB-INF/views/welcome.jsp) for rendering. During rendering, Struts2 tags can be used to access data from the action.
Struts2's Role in the MVC Pattern
Struts2 adheres strongly to the Model-View-Controller (MVC) architectural pattern, clearly separating concerns within a web application:
- Model: In Struts2, the Model is typically represented by JavaBeans or POJOs (Plain Old Java Objects) that hold application data and business logic. While actions themselves can contain data, it's common practice to use separate domain objects or service layers for complex business operations and data persistence. These model objects are often exposed to the view through the action's properties.
- View: The View is responsible for rendering the user interface. Struts2 supports various view technologies, including JSP, FreeMarker, Velocity, and XSLT. The view receives data from the Model (via the Action) and displays it to the user.
- Controller: The Controller component in Struts2 has two main parts:
- Front Controller: The
StrutsPrepareAndExecuteFilteracts as the central front controller, intercepting all requests, dispatching them to the appropriate action, and managing the overall request lifecycle. - Action Controller: Each Struts2 Action class (like our
WelcomeAction) serves as a command controller. It receives user input, interacts with the Model, processes the request, and determines which View should be rendered based on the outcome of its execution.
- Front Controller: The