Skip to content

WW-5849 Add a forward result to replace action chaining - #2129

Merged
lukaszlenart merged 7 commits into
mainfrom
feat/WW-5849-forward-result
Dec 2, 2026
Merged

lukaszlenart merged 7 commits into
mainfrom
feat/WW-5849-forward-result

Conversation

@lukaszlenart

@lukaszlenart lukaszlenart commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Fixes WW-5849

Adds a forward result: a server-side transfer from one action to another within the same request, chosen by the source action's result code. It is meant to replace chain, with both boundaries made explicit:

  • which actions can be forwarded to: the target opts in;
  • what data crosses: values are named on both sides.

chain is unchanged in this PR. Its deprecation (7.7.0) and removal (8.0.0) are separate tickets. The design is in docs/superpowers/specs/2026-11-08-WW-5849-action-forward-design.md and the implementation plan in docs/superpowers/plans/2026-12-02-WW-5849-forward-result.md.

What it does

<action name="order" class="...OrderAction">
    <result name="invoice" type="forward">
        <param name="actionName">invoice</param>
        <param name="pass">orderId</param>
    </result>
</action>

<action name="invoice" class="...InvoiceAction" forward-target="true"/>
@ForwardTarget
public class InvoiceAction {
    @ForwardParameter
    public void setOrderId(Long orderId) { ... }
}
  • Static target. actionName, namespace and method are literals. A ${...}, %{...} or wildcard/pattern placeholder {x} fails configuration at startup in every mode. ActionConfigMatcher copies forward-result params verbatim, so no URL or pattern variable reaches them. An omitted namespace means the source action's package namespace, not the request's.
  • The target opts in. Either @ForwardTarget on the class (not @Inherited, read from the class behind any proxy) or forward-target="true" on <action>. That attribute is the only grammar change, so it comes with a new struts-7.6.dtd. Forwarding to an action that has not opted in, or to a name that only resolves through default-action-ref, throws.
  • Fresh value stack and context. The source action is not on the target's stack. Errors and messages do not carry over. The request's value-stack attribute is swapped for the target's during the forward. The target sees the same request, session, application, locale and action mapping.
  • Request parameters re-bind under the target's own @StrutsParameter rules, like a servlet forward.
  • Explicit hand-off.
    • The source names the values it passes in pass. They are read through bean getters, with no OGNL evaluation.
    • The target accepts them only on top-level setters annotated @ForwardParameter, applied by the new forwardParams interceptor.
    • forwardParams runs directly after params in basicStack, defaultStack, paramsPrepareParamsStack (after the second params) and the REST and Bean Validation stacks.
    • Anything not accepted, or not convertible, is dropped with a WARN.
    • A forward with a hand-off to a target whose stack lacks forwardParams throws.
  • Depth limit. struts.forward.maxDepth defaults to 5 and also holds across mixed forward/chain loops.
  • Startup check (ForwardConfigValidator):
    • Invalid pass names fail startup in every mode.
    • Missing targets, targets that have not opted in, a missing forwardParams, and cycles in the forward graph are logged at WARN, or fail startup in devMode.
  • Convention. The implicit foo → foo-bar transfer becomes a forward when foo-bar has opted in. @Result(type = "forward") works.
  • tokenSession. Replaying a duplicate submit renders the forward target's result against the target's stack, and never re-runs a target that rendered nothing.

Decisions beyond the design doc

  • Convention keeps chaining to a foo-bar that has not opted in, so existing Convention apps keep working until chain is removed in 8.0.0. A package that does not extend struts-default also keeps the chain.
  • The cycle check covers explicit forward results only. An implicit forward always goes from foo to the longer foo-bar, so implicit forwards alone cannot form a cycle.
  • basicStack gets forwardParams too.
  • The startup check also validates target existence, the opt-in and the pass names, besides cycles.
  • ActionForwardResult is final, so detecting forward results by class name stays exact.
  • Names:
    • result type forward (ActionForwardResult)
    • attribute forward-target
    • annotations @ForwardTarget and @ForwardParameter
    • param pass
    • interceptor forwardParams
    • constant struts.forward.maxDepth

New public API (all @since 7.6.0)

  • org.apache.struts2.result.ActionForwardResult, NestedActionResult
  • org.apache.struts2.forward.ForwardTarget, ForwardParameter, ForwardTargets, ForwardContext, ForwardConfigValidator
  • org.apache.struts2.interceptor.ForwardParametersInterceptor
  • ActionInvocation#getResultInvocation(): a default method returning this. DefaultActionInvocation returns the innermost forwarded invocation.
  • ProxyService#ultimateTargetClassOf(Class<?>): a default method returning the argument. StrutsProxyService unwraps Spring/CGLIB proxy classes. It has a distinct name, not an overload, so existing any() call sites and stubs keep binding to ultimateTargetClass(Object).

Behaviour notes for the docs (struts-site PR to follow)

  • forward is not dispatcher. dispatcher is a servlet forward to a resource.
  • A handed-off value is not visible in prepare(), because forwardParams runs after prepare.
  • After the forward returns, filters such as SiteMesh decorators see the source action's stack, not the target's.
  • Put token/tokenSession on the source. With execAndWait on the target, every poll re-runs the source.
  • Parameters that only chain accepts, such as skipActions, fail on a forward result.
  • Migration from chain, step by step:
    1. Opt in the target.
    2. Add pass for the values the source must hand over.
    3. Change type="chain" to type="forward".
    4. Drop the chain interceptor reference.

Testing

./mvnw test -DskipAssembly on core: 3948 tests, 0 failures. Convention: 102. REST: 253. Spring: 61. Bean Validation: 26.

🤖 Generated with Claude Code

@lukaszlenart
lukaszlenart force-pushed the feat/WW-5849-forward-result branch from 9a9d578 to df56dc4 Compare December 2, 2026 13:14
@lukaszlenart lukaszlenart changed the title WW-5849 Design a forward result to replace action chaining WW-5849 Add a forward result to replace action chaining Dec 2, 2026
@lukaszlenart
lukaszlenart marked this pull request as ready for review December 2, 2026 13:20
lukaszlenart and others added 7 commits December 2, 2026 15:09
…ning

The design: a forward result with a target-side opt-in, a fresh value
stack, request parameters re-bound, an explicit two-sided hand-off, a
depth limit with a startup cycle check, and Convention's implicit
forward to opted-in targets; chain is deprecated in 7.7.0 and removed
in 8.0.0. The plan breaks rollout step 1 (7.6.0) into tasks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…limit

The forward result runs an opted-in target action in the same request
with its own value stack and ActionContext. The target keeps the
request, session, application, locale, action mapping and page context,
but nothing from the source action. Nested forwards are capped by
struts.forward.maxDepth (default 5). A tokenSession replay renders the
target's result against the target's stack, through
ActionInvocation#getResultInvocation(), and restores the previously
bound context afterwards.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The forward result's pass param names the source action's properties
to hand off; they are read through bean getters, never OGNL. The new
forwardParams interceptor sets each value only on a top-level setter of
the target annotated @ForwardParameter, converting it like a request
parameter; anything else is dropped with a warning. The hand-off is
consumed once, so it cannot reach an action the target chains to.
forwardParams runs directly after params in basicStack, defaultStack,
paramsPrepareParamsStack (after the second params) and the REST and
Bean Validation stacks; a hand-off to a stack without it throws.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ForwardConfigValidator runs when the configuration is built. A forward
whose target, namespace or method is not a literal (an expression or a
pattern placeholder) or whose pass names are invalid always fails
startup. Missing targets, targets that have not opted in, a hand-off to
a stack without forwardParams, and cycles in the forward graph are
logged at WARN, or fail startup in devMode.

The run time follows the same rules. ActionConfigMatcher copies forward
result params verbatim, so no URL or pattern variable reaches them; an
omitted namespace means the source action's package namespace; a target
that only resolves through default-action-ref is refused; an invalid
result param fails instead of being ignored; and ActionForwardResult is
final. Target classes behind Spring proxies are unwrapped through the
new ProxyService#ultimateTargetClassOf(Class). A tokenSession replay no
longer re-runs a forward target that rendered nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Convention's implicit foo -> foo-bar transfer becomes a forward when
foo-bar has opted in, through the forward-target flag or @ForwardTarget
on its class (proxies unwrapped). Otherwise it keeps chaining as before,
so existing Convention applications work unchanged until chain is
removed in 8.0.0. @Result(type = "forward") is supported.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lukaszlenart
lukaszlenart force-pushed the feat/WW-5849-forward-result branch from 7b31a47 to d846ac1 Compare December 2, 2026 14:25
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@lukaszlenart
lukaszlenart merged commit 1ded7bb into main Dec 2, 2026
8 checks passed
@lukaszlenart
lukaszlenart deleted the feat/WW-5849-forward-result branch December 2, 2026 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant