Fork me on GitHub
Edit on GitHub << back to Result types

Forward Result

Since Struts 7.6.0

Description

The forward result runs another action in the same request, without a round-trip to the browser. The source action picks the branch by returning a result code, and the target action’s own result renders the response. It is the replacement for Action Chaining: unlike chain, both boundaries of the transfer are explicit.

Do not confuse it with the Dispatcher Result, which is a servlet forward to a resource such as a JSP. The forward result runs a whole action, with its own interceptor stack and result.

Parameters

Targets are never computed from data: ${...} and %{...} expressions are not evaluated, and placeholders like {1} from a wildcard or regex action mapping are not substituted. Such a value fails the configuration at startup.

Example

<package name="shop" extends="struts-default">
    <action name="order" class="com.example.OrderAction">
        <result name="success">/WEB-INF/order.jsp</result>
        <result name="invoice" type="forward">
            <param name="actionName">invoice</param>
            <param name="pass">orderId</param>
        </result>
    </action>

    <action name="invoice" class="com.example.InvoiceAction">
        <result>/WEB-INF/invoice.jsp</result>
    </action>
</package>
@ForwardTarget
public class InvoiceAction extends ActionSupport {

    private Long orderId;

    @ForwardParameter
    public void setOrderId(Long orderId) {
        this.orderId = orderId;
    }

    public Long getOrderId() {
        return orderId;
    }
}

When OrderAction returns invoice, InvoiceAction runs in the same request with orderId handed off, and invoice.jsp renders against InvoiceAction.

Opting in a target

An action can only be forwarded to if it is marked as a forward target. Everything else is refused, the same allowlist model as @StrutsParameter uses for request parameters. There are two equivalent ways:

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 7.6//EN"
        "https://struts.apache.org/dtds/struts-7.6.dtd">
<action name="invoice" class="com.example.InvoiceAction" forward-target="true">
    <result>/WEB-INF/invoice.jsp</result>
</action>

Opting in matters even though the hand-off is explicit. A forward bypasses everything mapped to the target’s URL, such as web.xml security constraints, servlet filters and container authentication. Only the target’s own interceptors run.

Forwarding to an action that has not opted in throws an exception. So does forwarding to a name that does not exist, even when the namespace defines a default-action-ref.

What the target sees

Handing off values

The source lists the properties it hands off in pass. Each one is read through the source action’s getter; the names are simple property names, never expressions. The target accepts a value only on a top-level setter annotated with @org.apache.struts2.forward.ForwardParameter. Nested paths and the ModelDriven model are not supported.

Hand-off values are applied by the forwardParams interceptor, which runs directly after params in every stack shipped with the framework: basicStack, defaultStack, paramsPrepareParamsStack (after the second params), and the REST and Bean Validation plugin stacks.

Handed-off values arrive after prepare, so Preparable.prepare() cannot use them yet. Load data that depends on a handed-off value in the action method instead.

Depth limit

Nested forwards in one request are limited by the struts.forward.maxDepth constant, which defaults to 5. Exceeding it throws an exception. The limit also holds when forwards and chains are mixed in a loop.

<constant name="struts.forward.maxDepth" value="3"/>

Startup checks

Because targets are static, the configuration is checked when the application starts:

Convention plugin

The Convention plugin transfers from action foo to foo-bar when foo returns bar and has no matching result. When foo-bar has opted in as a forward target, this transfer is a forward. Otherwise it is still a chain, as before. The implicit forward hands off no values, only request parameters.

To hand off values, declare the result explicitly:

@Action(value = "foo", results = {
    @Result(name = "bar", type = "forward", location = "foo-bar", params = {"pass", "orderId"})
})

Interaction with other interceptors

Migrating from chain

The chain result is planned to be deprecated in Struts 7.7.0 and removed in Struts 8.0.0. To move a chain to a forward:

  1. Opt in the target action with @ForwardTarget or forward-target="true".
  2. If the target relied on properties copied by the Chaining Interceptor, list them in pass on the source’s result and annotate the target’s setters with @ForwardParameter. Many chains need no hand-off at all, because request parameters are bound again.
  3. Change type="chain" to type="forward". Remove parameters that only chain accepts, such as skipActions: an unknown parameter fails on a forward result. Replace any ${...} expression in actionName, namespace or method with a literal, and use one forward result per result code instead. If namespace is omitted, chain uses the namespace of the current request while forward uses the namespace of the source action’s package; set namespace explicitly where the two differ, for example with a wildcard namespace or the empty-namespace fallback.
  4. Remove the chain interceptor reference from the target’s stack if you added one.

Before:

<action name="createAccount" class="...">
    <result type="chain">login</result>
</action>
<action name="login" class="...">
    <interceptor-ref name="chain"/>
    <interceptor-ref name="defaultStack"/>
    <result>/WEB-INF/login.jsp</result>
</action>

After, with a getUsername() getter on the createAccount action and a setter annotated with @ForwardParameter on the login action (without that setter the value is dropped and logged at WARN):

<action name="createAccount" class="...">
    <result type="forward">
        <param name="actionName">login</param>
        <param name="pass">username</param>
    </result>
</action>
<action name="login" class="..." forward-target="true">
    <result>/WEB-INF/login.jsp</result>
</action>
Follow @x