I have a HandlerInterceptor to add some “global” model variables. It works.
Now, I try to reuse it in Spring Web Flow, for the same reason.
But HandlerInterceptors have the ModelAndView parameter set to NULL under Spring Web Flow (couldn’t figure why, but it’s a fact).
I have referenced my interceptor in the FlowHandlerMapping bean :
<bean class="org.springframework.webflow.mvc.servlet.FlowHandlerMapping">
<property name="order" value="0" />
<property name="flowRegistry" ref="flowRegistry" />
<property name="interceptors">
<list>
<ref bean="myInterceptor" />
</list>
</property>
</bean>
How can I add variables to the model ?
Is there a workaround, with the request parameter for example ?
Starting with Spring Webflow 2, the
ModelAndViewobject is not generated anymore (see this post (and thread) at the SpringSource forum).The
FlowHandlerAdapterhandle() function does not generate a ModedAndView anymore (it just returns null) even if this function is :So overriding this function is pointless, but this function creates a
ServletExternalContextobject, which holds all the flow variable, by calling its method :By overriding this function you can pretty much do what you want with this flow variables.
To do this, just create a class that extends
FlowHandlerAdapter, register it instead of FlowHandlerAdapter and override thecreateServletExternalContextfunction.Basically you use
ServletExternalContext.getSessionMap()to access aSharedAttributeMapand register your properties.As you have access to the
HttpServletRequestandHttpServletResponseobjects, this method can act petty much like aHandlerInterceptorAdapter.postHandlefunction.See an example below.
I left out how to use generic way to reuse the same code for a
HandlerInterceptorfor the MVC and this object but it’s easy to code, by implementingHandlerInterceptor.MyFlowHandlerAdapter :
You have the
FlowHandlerAdapterobject defined in you webflow-context.xml file like that :Just replace it with :