java - 在没有 <mvc :annotation-driven/> in Spring MVC 3. 1 的情况下使用 Flash 范围(和 RedirectAttributes)

标签 java spring-mvc

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

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

因为我不希望拦截器应用于资源并且没有办法(我认为)指定将拦截器应用于除了这个和那个之外的所有内容的映射路径.

所以我必须添加自己的 RequestMappingHandlerMapping 才能在其上指定拦截器,而不是全局指定拦截器。因为thisthis ,看来我不能在保留 元素的同时简单地定义我自己的 RequestMappingHandlerMapping!

所以……用some help ,我已经能够摆脱 元素,现在几乎一切正常。我的拦截器应用于除我的资源以外的所有内容。一切正常,闪光灯除外!

@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 map 为 NULL,我的模型不包含我的变量。当我尝试使用 元素时,它运行良好!所以我的问题是:我的上下文中缺少什么来使 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" />

闪光示波器缺少什么才能工作?

更新:查看我的解决方案答案...实际上什么都没有丢失。只有 Session 没有正常工作,我找到了让它工作的方法。

最佳答案

我终于找到了闪光瞄准镜工作所缺少的东西!

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

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

而不是这个:

public String test2(Model model, HttpServletRequest request)

这似乎使 Session 正常工作,因此也使 flash 示波器正常工作!为什么?我不知道……

关于java - 在没有 <mvc :annotation-driven/> in Spring MVC 3. 1 的情况下使用 Flash 范围(和 RedirectAttributes),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9960410/

相关文章:

java - 在Spring中生成一致的资源链接

java - 如何使用拦截器 Spring mvc 在 session 过期后重定向

java - 如何在beans.xml中使用@JsonPropertyOrder注释

java - 我应该如何测试 Kotlin 扩展功能?

java - 如何在 jnr ffi 中使用结构体和结构体

java - SVD 具有与 WolframAlpha 不同的解决方案

java - JSP 形式中具有多个属性的 OneToMany 关系

java - Tomcat 8.0 不会以纯 Spring MVC 模板启动

java - 如何在不同的类中访问此关键字

java - 在与非托管对象具有持久关联的 jpa 实体 : You cannot flush unmanaged objects. 中设置外键时出错