不使用Flash作用域(和RedirectAttributes) 在Spring MVC 3.1中


问题内容

在我的Spring MVC 3.1应用程序中,我认为我不能使用“ ”。为什么?因为我想将拦截器应用于除“
<mvc:resources”元素以外的 所有 映射。所以我不能使用:

<mvc:annotation-driven />

<mvc:resources order="-10" mapping="/public/**" location="/public/" />
<mvc:resources order="-10" mapping="/favicon.ico" location="/public/favicon.ico" />
<mvc:resources order="-10" mapping="/robots.txt" location="/public/robots.txt" />
<mvc:resources order="-10" mapping="/humans.txt" location="/public/humans.txt" />

<mvc:interceptors>  
  <mvc:interceptor>
    <mvc:mapping path="/**"/>
    <bean class="com.my.Interceptor" />
  </mvc:interceptor>
</mvc:interceptors>

因为我不希望拦截器适用于资源,并且(我认为)没有办法为映射指定路径,该路径会将拦截器应用于 除this和that之外的所有内容

因此,我必须添加自己的 RequestMappingHandlerMapping
才能在其上指定拦截器,而不是全局地指定拦截器。因为这个这个,我似乎不能简单地定义自己的 RequestMappingHandlerMapping
同时保持 _< MVC:注解驱动/>_元素!

所以…在一些帮助下,我已经摆脱了 _< mvc:annotation-driven
/>_元素,现在几乎所有东西都运行良好。我将拦截器应用于除资源以外的所有内容。一切正常, 除了闪光灯范围

@RequestMapping(value="/test", method=RequestMethod.POST)
public String test(Model model, RedirectAttributes redirectAttrs) 
{
    redirectAttrs.addFlashAttribute("myKey", "my message");
    return "redirect:test2";
}

@RequestMapping(value="/test2")
public String test2(Model model, HttpServletRequest request) 
{
    Map<String, ?> map = RequestContextUtils.getInputFlashMap(request); // map is NULL
    System.out.println(model.containsAttribute("myKey")); // Prints FALSE
}

Flash映射为NULL,并且我的模型不包含我的变量。当我尝试使用 _< mvc:annotation-driven
/>_元素时,它运行良好!所以我的问题是:使Flash作用域正常工作的上下文中缺少什么?

我也尝试将“
org.springframework.web”设置为DEBUG日志记录级别,并且在重定向之后,没有任何与FlashMap或FlashMapManager相关的日志记录。似乎肯定缺少一些必需的bean。

这是上下文文件中有趣的部分:

<!-- commented! -->
<!-- <mvc:annotation-driven /> -->

<bean id="baseInterceptor" class="com.my.Interceptor" />

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="order" value="0" />
    <property name="interceptors">
        <list>
            <ref bean="conversionServiceExposingInterceptor" />
            <ref bean="baseInterceptor" />
        </list>
    </property>
</bean>

<bean id="myRequestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
    <property name="webBindingInitializer">
        <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
            <property name="conversionService" ref="conversionService" />
            <property name="validator" ref="validator" />
        </bean> 
    </property>
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
            <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
        </list>
    </property>
</bean>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" />
<bean id="conversionServiceExposingInterceptor" class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor">
    <constructor-arg ref="conversionService" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:/messages/messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"></bean>

<mvc:resources order="-10" mapping="/public/**" location="/public/" />
<mvc:resources order="-10" mapping="/favicon.ico" location="/public/favicon.ico" />
<mvc:resources order="-10" mapping="/robots.txt" location="/public/robots.txt" />
<mvc:resources order="-10" mapping="/humans.txt" location="/public/humans.txt" />

Flash示波器工作时缺少什么?

更新 :请参阅我的解决方案答案…实际上什么都没丢失。只有Session无法正常工作,我找到了使其正常工作的方法。


问题答案:

我终于找到了闪存示波器工作所需的东西!

在我访问flash变量的操作中(在重定向导致的页面上),我必须使用以下命令:

public String test2(Model model, HttpServletRequest request, HttpSession session)

代替这个:

public String test2(Model model, HttpServletRequest request)

看来这使Session正常工作,因此也使Flash作用域也正常工作!为什么?我不知道…