如何在 Spring Web Flow 侦听器中检测重定向?
我有一个使用 Spring 框架、Velocity 和(部分)Spring Web Flow 编写的 Web 应用程序。
为了在我的所有网页中为速度模板提供一组通用的引用,我编写了一个 FlowExecutionListener,如下所示:
@Override
public void requestSubmitted(RequestContext context)
{
Map<String, Object> model = new HashMap<String, Object>();
populateModel(model);
context.getRequestScope().putAll(new LocalAttributeMap(model));
}
然后我可以访问模型中存储的键作为 Velocity 引用,例如 $reference< /代码>。 一般来说,这效果很好。
但是,流程中的某些退出点将重定向到 GET 请求,如下所示:
<view-state id="startOver" view="redirect:/new"/>
这些重定向将对新请求执行 302,所有请求属性都转换为 URL 参数,从而产生一个大而笨重的 URL,而不是干净的 < code>/new 我试图重定向到的网址。
我想在监听器中检测流何时返回重定向,以便我可以跳过请求属性中字符串的存储。 (我使用标准 Spring MVC 的拦截器通过匹配视图名称是否以“redirect:”开头来执行此操作)。但我似乎无法在侦听器中找到一种方法来检测这一点。
或者,是否有其他方法可以防止重定向后将请求属性放在 URL 上?
I have a webapp written with the Spring Framework, Velocity, and (in part) Spring Web Flow.
In order to provide a common set of references in all my web pages for my velocity template, I have written a FlowExecutionListener which looks like this:
@Override
public void requestSubmitted(RequestContext context)
{
Map<String, Object> model = new HashMap<String, Object>();
populateModel(model);
context.getRequestScope().putAll(new LocalAttributeMap(model));
}
I can then access the keys stored in the model as Velocity references, e.g. $reference
.
In general, this works great.
However, certain exit points from the flow will redirect to a GET request, like so:
<view-state id="startOver" view="redirect:/new"/>
These redirects will do a 302 to a new request, with all the request attributes turned into URL parameters, resulting in a big unwieldy URL instead of the clean /new
url I was trying to redirect to.
I would like to detect in the listener when the flow is returning a redirect, so that I can skip the storing of the strings in the request attributes. (I do this with an interceptor with standard Spring MVC by matching whether the view name starts with "redirect:" ). But I can't seem to find a way in the listener to detect this.
Alternately, are there other ways to prevent the request attributes from being put on the URL after a redirect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用自定义 FlowUrlHandler 来构建重定向的 URL。了解如何实现它的最佳方法是查看 DefaultFlowUrlHandler 的代码,但是您问题的主要思想是过滤来自 flowDefinitionUrl 的输入,例如:
要使用它,请使用以下 Spring 配置:
You can use a custom FlowUrlHandler which builds the URL for the redirect. The best way to see how to implement it is to look at the code of DefaultFlowUrlHandler, but the main idea for you problem is to filter the input from the flowDefinitionUrl, for example:
To use it, use this Spring configuration:
您使用的是哪个版本的 Spring webflow?
我在 1.0.5 上工作过,它有一个名为 alwaysRedirectOnPause 的属性,它会发出重定向。您可以通过使用类似这样的标签将其设置为 false
您还可以阅读有关此重定向行为如何帮助您处理重复提交的更多详细信息
在 理解 alswaysRedirectOnPause
我认为即使 Spring webflow 2 也有类似的属性,但我我不知道确切的语法。
希望这有帮助。
Which version of Spring webflow are you using?
I have worked on 1.0.5 and it has a attribute called alwaysRedirectOnPause which issues redirects. You can set this to false by using tag something like this
Also you can read more details of how this redirect behavior helps you on duplicate submits
at Understanding alswaysRedirectOnPause
I think even Spring webflow 2 has similar attribute, but I am not aware of exact syntax.
Hope this helps.