【问题标题】:How can I access Spring Webflow FlowScope elements outside the flow?如何访问流外的 Spring Webflow FlowScope 元素?
【发布时间】:2026-01-21 16:40:02
【问题描述】:

编辑:我对这个问题没有任何了解,所以我添加了更多细节。

我有一个 Spring Webflow 应用程序(2.3.2 版)。我需要从其中一个步骤的验证内部访问多个 FlowScope 对象(而不是在流程本身内部)。你会认为这很简单,但我无法破解它。

Spring Webflow 提供了一系列special EL variables 可用于访问各种范围,但仅限于内部流本身。在自定义 Spring 验证器中,似乎没有任何方法可以访问它们:

@Component
public class MyObjectValidator {

    @Autowired
    ApplicationContext context;

    public void validateMyObject(MyObject myObject, Errors errors) {

        FlowScope flowScope = context.someMagicFunction();  //  <---- UNKNOWN  
        MyOtherObject otherObject = flowScope.get("otherObject");  

        if (incrediblyComplexValidation(myObject, otherObject) {
            errors.rejectValue("myField","validation.fail","Your object failed validation.");
        }
    }
}

如您所见,在Spring Webflow Validator 中,除了您应该验证的对象之外,没有与任何东西的直接外部链接。我需要找到 flowScope 中的其他对象。理想情况下,无论是通过ApplicationContext 还是其他一些环境特征,都必须有一种方法可以到达这些其他对象。

有人知道答案吗?

【问题讨论】:

    标签: java spring spring-webflow-2


    【解决方案1】:

    您可以从 RequestContext 获取范围 bean - 请求特定状态当前 Web 应用程序上下文的上下文持有者。通过以下方式访问验证器方法中的请求上下文:

        import org.springframework.webflow.execution.RequestContext;
        import org.springframework.webflow.execution.RequestContextHolder;
    
        RequestContext requestContext = RequestContextHolder.getRequestContext();
        requestContext.getFlowScope().get("objectYouAreLookingFor");
    

    【讨论】:

    • 当我尝试获取值时得到 null。你知道如何传递请求上下文