开发者

How to detect redirect in Spring Web Flow listener?

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开发者_JS百科(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?


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜