Skip to content
javahacks.dev
Struts

What Struts 2 Is and How an Action Request Flows Through

Follow a request through the Struts 2 filter, action mapping, interceptor stack, your action and the result, and see how it compares with Spring MVC.

6 min read
Summarize in AI
Flat illustration of a request passing through a servlet container, with the Spring, NGINX, Docker, Apache Tomcat and Apache Maven logos.

Struts 2 is an action-based MVC framework, built on the idea that a request is handled by a plain Java object created for that request. It has been around since the mid 2000s, and a large number of production applications still run on it, so understanding how a request moves through it is the quickest way to debug and secure one. This post follows one request from the servlet container to the rendered page, naming each component on the way. It applies to Struts 6 and 7, and the configuration details are in configuring actions and results.

The path of one request

A browser requests /shop/save.action. These steps follow, in order:

  1. A servlet filter receives the request and decides it is for Struts.
  2. The framework maps the URL to an action configuration: a namespace, an action name and a method.
  3. It creates a new instance of the action class.
  4. It runs an interceptor stack around the action; interceptors bind parameters, validate and do other work.
  5. It calls the action method, which returns a string such as success or input.
  6. It looks up the result configured for that string and renders it, usually a JSP.

Nothing here is held between requests. Each request gets its own action instance, which is why action fields are safe for request data, and the contrast with singleton controllers in other frameworks is the first thing to remember.

The filter

Struts plugs into the servlet container with a filter, mapped to all URLs:

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 filter asks whether the request matches a configured action. If it does, Struts handles it. If not, the request passes down the chain to other servlets and static content. A filter is used and not a servlet because it can decline a request, so images and stylesheets are untouched. The mapping of a URL to a namespace, an action name and an extension is the job of the ActionMapper, which strips the .action suffix and the optional method.

The action and the config

java
public class ProductAction extends ActionSupport {

    private String name;
    private long priceCents;

    public String save() {
        productService.save(name, priceCents);
        return SUCCESS;
    }

    // getters and setters for name and priceCents
}
XML
<action name="save" class="dev.javahacks.web.ProductAction" method="save">
    <result type="redirectAction">list</result>
    <result name="input">/product-form.jsp</result>
</action>

The framework creates a ProductAction, copies the submitted form fields into name and priceCents through their setters, then calls save(). The returned string selects the result. ActionSupport supplies the standard result names, validation hooks and message support. Services are injected into the action by a plugin such as Spring, or looked up by a factory; the action itself should stay thin.

Interceptors do the cross-cutting work

Interceptors wrap the action in a chain, like servlet filters scoped to one action. A default stack, applied to every action in a package that extends struts-default, includes the interceptors that do most of what you take for granted. One binds request parameters onto action properties. One runs validation. One handles file uploads. One converts exceptions into results. One keeps the input flow working by returning to the form when validation fails. Because they run before the action, they are also where attacks enter: a parameter binding interceptor that will set any property named in the request is the origin of several well-known vulnerabilities, discussed in hardening Struts 2 and the security bulletins. Know which interceptors run for each action, and put your own, such as authentication, first in the stack.

The result and the value stack

The returned string selects a result, and the common types are dispatcher to a JSP, redirect and redirectAction. The view reads data through the value stack, a stack of objects that begins with the action instance. A tag in the JSP names a property and the stack finds it by searching from the top:

XML
<s:property value="name" />
<s:textfield name="priceCents" />

The expressions are written in OGNL, a small expression language that can read properties, call methods and index collections. That power makes OGNL a security concern, since an attacker who can influence an expression that is evaluated can run code. Never build an OGNL expression from user input, and see using OGNL safely. The value stack is also why a form shows the user's input again after a validation error: the action still holds the submitted values, and the textfield reads them back.

Where the comparison with Spring MVC stops

Both are action-oriented and both put a front controller before your code, as the contrasting description in how Spring MVC handles a request shows. They differ in the unit of work. Struts creates an action object per request and binds the request onto its fields. Spring MVC calls a method on a singleton controller and binds the request onto method arguments, so request data never lives in a shared object's fields. That makes Spring MVC code easier to reason about for concurrency, and it is the reason most migrations away from Struts head in that direction.

Following a request in a debugger

The quickest way to learn the flow is to watch it. Put a breakpoint in an action's setter for a form field and run the request. The stack trace above it lists the interceptor that called the setter, the ActionInvocation that drove the chain and the dispatcher at the bottom. Step outwards and you see the order of the stack. Do the same in execute to see which interceptors are still on the stack when your code runs, and in a result's execute to see that the interceptor chain has already unwound by the time the page is produced, with the PreResultListener callbacks in between.

Development mode helps too. With struts.devMode set to true, the framework reloads configuration, logs more about parameter binding, and gives detailed error pages. Parameters that were rejected, because they were not allowed by the parameter filters, are logged there, which explains many reports of a form field that is never set. Switch it off in production.

Why the per-request action matters in practice

Because the action is new for each request, fields hold request-scoped state with no synchronisation. The same fact means expensive objects must not be created in a constructor or field initialiser, since that cost is paid on every request. Keep the action light, take dependencies by injection from a long-lived container, and put anything heavy in a service. Applications that ignored this showed up as slow pages whose profile was dominated by action construction.

If you maintain one of these applications

Keep the framework on a supported version, since vulnerabilities in older ones have been exploited widely. Turn on strict method invocation and restrict the allowed methods. Review each interceptor stack. The official developer documentation covers every component named here.

Also inSpring MVC
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.