【问题标题】:Using a HandlerInterceptor to add model attributes with Spring Web Flow使用 HandlerInterceptor 通过 Spring Web Flow 添加模型属性
【发布时间】:2012-03-30 00:48:56
【问题描述】:

我有一个 HandlerInterceptor 来添加一些“全局”模型变量。有用。

现在,出于同样的原因,我尝试在 Spring Web Flow 中重用它。

但是 HandlerInterceptors 在 Spring Web Flow 下的 ModelAndView 参数设置为 NULL(不知道为什么,但这是事实)。

我在 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>

如何向模型添加变量?

是否有解决方法,例如请求参数?

【问题讨论】:

    标签: spring-mvc spring-webflow-2


    【解决方案1】:

    从 Spring Webflow 2 开始,不再生成 ModelAndView 对象(请参阅 SpringSource 论坛上的 this post(和线程)。


    FlowHandlerAdapterhandle() 函数不再生成 ModedAndView(它只是返回 null),即使此函数是:

    public ModelAndView handle(HttpServletRequest request, 
            HttpServletResponse response, Object handler)
    

    所以重写这个函数是没有意义的,但是这个函数通过调用它的方法创建了一个ServletExternalContext对象,它保存了所有的流变量:

    protected ServletExternalContext createServletExternalContext(
        HttpServletRequest request, HttpServletResponse response)
    

    通过覆盖这个函数,你几乎可以用这个流变量做你想做的事。


    为此,只需创建一个扩展 FlowHandlerAdapter 的类,注册它而不是 FlowHandlerAdapter 并覆盖 createServletExternalContext 函数。

    基本上,您使用ServletExternalContext.getSessionMap() 访问SharedAttributeMap 并注册您的属性。

    由于您可以访问HttpServletRequestHttpServletResponse 对象,因此此方法的作用与HandlerInterceptorAdapter.postHandle 函数非常相似。

    请参阅下面的示例。

    我省略了如何使用通用方式为 MVC 和这个对象的 HandlerInterceptor 重用相同的代码,但通过实现 HandlerInterceptor 很容易编码。


    MyFlowHandlerAdapter:

    package my.package;
    public class MyFlowHandlerAdapter extends FlowHandlerAdapter {
    
        @Override
        protected ServletExternalContext createServletExternalContext(
                HttpServletRequest request,
                HttpServletResponse response) {
    
            ServletExternalContext context = 
                super.createServletExternalContext(request,response);
    
            context.getSessionMap().put("myproperty", "myvalue");
    
            return context;
        }
    }
    

    您在 webflow-context.xml 文件中定义了 FlowHandlerAdapter 对象:

    <bean class="org.springframework.webflow.mvc.servlet.FlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor"/>
    </bean>
    

    只需将其替换为:

    <bean class="my.package.MyFlowHandlerAdapter">
        <property name="flowExecutor" ref="flowExecutor"/>
    </bean>
    

    【讨论】:

    • 您好,我在 Spring Web Flow 中集成拦截器时遇到问题,您能帮帮我吗?
    【解决方案2】:

    ModelAndView 可以在拦截器处理 Ajax 请求时为空。

    只需检查 ModelAndView 是否为 null。如果没有,那是因为拦截器正在处理一个视图模型,所以你可以在这个时候添加你的变量。

    【讨论】:

    • 没有控制器:它是一个网络流! :-)
    • 实际上,我找到了一个“干净”的解决方法:我创建了一个 @Service 注释类,它由 MVC 组件的 HandlerInterceptor 调用,并在 SWF 组件的流中调用。这以某种方式避免了代码重复。
    猜你喜欢
    • 2019-01-12
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 2012-03-02
    • 2016-02-15
    • 2011-02-16
    • 2018-06-26
    • 1970-01-01
    相关资源
    最近更新 更多