Forward Result
Since Struts 7.6.0
- Description
- Parameters
- Example
- Opting in a target
- What the target sees
- Handing off values
- Depth limit
- Startup checks
- Convention plugin
- Interaction with other interceptors
- Migrating from chain
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.
- Which actions can be forwarded to: only actions that opted in as a forward target.
- What data crosses: only values the source names and the target accepts. The target gets a fresh value stack, so the source action is not visible to it or to its result.
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
actionName(default) - the name of the target action. It must be a literal.namespace- the target’s namespace. It must be a literal. If omitted, it defaults to the namespace of the package the source action is declared in.method- the method to invoke on the target. It must be a literal. If omitted, the target’s configured method is used.pass- a comma-separated list of the source action’s properties to hand off to the target, see Handing off values.
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:
- Annotate the class with
@org.apache.struts2.forward.ForwardTarget. The annotation is not inherited, so a base class cannot opt in all its subclasses. It is read from the class behind a Spring proxy. - Set
forward-target="true"on the<action>element. Use this form for classes you cannot annotate. The attribute needs the 7.6 DTD:
<!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
- A fresh value stack and its own
ActionContext. The source action is not on the target’s stack. Action errors, field errors and messages do not carry over unless you hand them off explicitly. - The same request. The target sees the same request, session, application, locale and action mapping. Request
parameters are bound again by the target’s
paramsinterceptor, under the target’s own@StrutsParameterrules, like a servlet forward. - Its full interceptor stack. Security and HTTP-method interceptors see the original request.
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.
- When a request parameter and a handed-off value set the same property, the handed-off value wins.
- A value the target does not accept, or one that cannot be converted to the setter’s type, is dropped and logged at WARN.
- A forward that hands off values to a target whose stack has no
forwardParamsinterceptor throws, rather than silently dropping them. If your target uses a custom stack, add<interceptor-ref name="forwardParams"/>afterparams.
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:
- A target, namespace or method that is not a literal, or an invalid
passname, always fails startup. - A missing target, a target that has not opted in, a hand-off to a target without the
forwardParamsinterceptor, and a cycle of forwards are logged at WARN. In devMode they fail startup.
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
- Put
tokenortokenSessionon the source action. A replayed duplicate submit renders the target’s result again without running the target a second time. execAndWaitkeys its background job to the action it runs on. WithexecAndWaiton the target, the wait page polls the source action’s URL, so each poll goes through the source action again before reaching the target. Prefer puttingexecAndWaiton the source.- After the forward returns, filters such as SiteMesh decorators see the source action’s value stack, not the target’s.
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:
- Opt in the target action with
@ForwardTargetorforward-target="true". - If the target relied on properties copied by the Chaining Interceptor, list them in
passon 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. - Change
type="chain"totype="forward". Remove parameters that onlychainaccepts, such asskipActions: an unknown parameter fails on aforwardresult. Replace any${...}expression inactionName,namespaceormethodwith a literal, and use oneforwardresult per result code instead. Ifnamespaceis omitted,chainuses the namespace of the current request whileforwarduses the namespace of the source action’s package; setnamespaceexplicitly where the two differ, for example with a wildcard namespace or the empty-namespace fallback. - Remove the
chaininterceptor 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>