Java 如何访问流之外的 Spring Webflow FlowScope 元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22462077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I access Spring Webflow FlowScope elements outside the flow?
提问by user3120173
EDIT: I didn't get any bites on this question, so I'm adding a little more detail.
编辑:我对这个问题没有任何意见,所以我添加了更多细节。
I have a Spring Webflow app (ver 2.3.2). I need to access multiple FlowScope objects from inside the validation of one of the steps (not inside the flow itself). You would think this would be simple, but I haven't been able to crack it.
我有一个 Spring Webflow 应用程序(版本 2.3.2)。我需要从其中一个步骤的验证内部(而不是在流程本身内部)访问多个 FlowScope 对象。你会认为这很简单,但我一直无法破解它。
Spring Webflow provides a series of special EL variableswhich can be used for accessing the various scopes, but only insidethe flow itself. Inside a custom Spring validator, there doesn't seem to be any way to get to them:
Spring Webflow 提供了一系列特殊的 EL 变量,可用于访问各种范围,但只能在流本身内部使用。在自定义 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.");
}
}
}
As you can see, inside a Spring Webflow Validatorthere is no direct external linkage to anything except the object you are supposed to validate. I need to get to those other objects in the flowScope. Ideally either through the ApplicationContext
or some other environmental feature there must be a way to get to these other objects.
如您所见,在Spring Webflow Validator 中,除了您应该验证的对象之外,没有与任何内容的直接外部链接。我需要访问 flowScope 中的其他对象。理想情况下,无论是通过ApplicationContext
还是其他一些环境特征,都必须有一种方法可以到达这些其他对象。
Anyone know the answer to this?
有谁知道这个问题的答案吗?
采纳答案by Prasad
You can get the scope beans from RequestContext - context holder for request-specific statecurrent web application context. Access to request context in your validator method is by:
您可以从 RequestContext 获取范围 bean - 请求特定状态当前 Web 应用程序上下文的上下文持有者。通过以下方式访问验证器方法中的请求上下文:
import org.springframework.webflow.execution.RequestContext;
import org.springframework.webflow.execution.RequestContextHolder;
RequestContext requestContext = RequestContextHolder.getRequestContext();
requestContext.getFlowScope().get("objectYouAreLookingFor");