如何在 Spring Web Flow 侦听器中检测重定向?

发布于 2024-10-14 02:49:57 字数 860 浏览 5 评论 0原文

我有一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暗喜 2024-10-21 02:49:57

您可以使用自定义 FlowUrlHandler 来构建重定向的 URL。了解如何实现它的最佳方法是查看 DefaultFlowUrlHandler 的代码,但是您问题的主要思想是过滤来自 flowDefinitionUrl 的输入,例如:

class MyFlowHandlerAdapter extends DefaultFlowHandlerAdapter {
  public String createFlowDefinitionUrl(String flowId, AttributeMap input, HttpServletRequest request) {
    MuttableAttributeMap filteredInput = new MuttableAttributeMap(input);
    // remove the velocity parameters
    filteredInput.remove("velocity.param1");
    // build the url
    return super.createFlowDefinitionUrl(flowId,filteredInput,request);
  }

}

要使用它,请使用以下 Spring 配置:

<bean id="myFlowUrlHandler" class="com.myapp.MyFlowHandlerAdapter">

<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
<bean id="flowHandlerAdapter" class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
  <property name="flowExecutor" ref="flowExecutor" />
  <property name="flowUrlHandler" ref="myFlowUrlHandler" />
  <property name="requireSession" value="true" />
</bean>

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:

class MyFlowHandlerAdapter extends DefaultFlowHandlerAdapter {
  public String createFlowDefinitionUrl(String flowId, AttributeMap input, HttpServletRequest request) {
    MuttableAttributeMap filteredInput = new MuttableAttributeMap(input);
    // remove the velocity parameters
    filteredInput.remove("velocity.param1");
    // build the url
    return super.createFlowDefinitionUrl(flowId,filteredInput,request);
  }

}

To use it, use this Spring configuration:

<bean id="myFlowUrlHandler" class="com.myapp.MyFlowHandlerAdapter">

<!-- Dispatches requests mapped to flows to FlowHandler implementations -->
<bean id="flowHandlerAdapter" class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
  <property name="flowExecutor" ref="flowExecutor" />
  <property name="flowUrlHandler" ref="myFlowUrlHandler" />
  <property name="requireSession" value="true" />
</bean>
星光不落少年眉 2024-10-21 02:49:57

您使用的是哪个版本的 Spring webflow?

我在 1.0.5 上工作过,它有一个名为 alwaysRedirectOnPause 的属性,它会发出重定向。您可以通过使用类似这样的标签将其设置为 false

<flow:alwaysRedirectOnPause value="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

<flow:alwaysRedirectOnPause value="false"/>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文