title: Using the Flash scope ( and RedirectAttributes ) without <mvc:annotation-driven /> in Spring MVC 3.1
body: <p>In my Spring MVC 3.1 application, I think I can't use "&lt;mvc:annotation-driven />". Why? Because I want to apply an interceptor to <em>all</em> mappings except to the "&lt;mvc:resources" elements. So I can't use :</p>
  
  <pre><code>&lt;mvc:annotation-driven /&gt;
  
  &lt;mvc:resources order="-10" mapping="/public/**" location="/public/" /&gt;
  &lt;mvc:resources order="-10" mapping="/favicon.ico" location="/public/favicon.ico" /&gt;
  &lt;mvc:resources order="-10" mapping="/robots.txt" location="/public/robots.txt" /&gt;
  &lt;mvc:resources order="-10" mapping="/humans.txt" location="/public/humans.txt" /&gt;
  
  &lt;mvc:interceptors&gt;  
    &lt;mvc:interceptor&gt;
      &lt;mvc:mapping path="/**"/&gt;
      &lt;bean class="com.my.Interceptor" /&gt;
    &lt;/mvc:interceptor&gt;
  &lt;/mvc:interceptors&gt;
  </code></pre>
  
  <p>Because I don't want the interceptor to apply to the resources and there is no way (I think) to specify a path for the mapping which would apply the interceptor to <strong>everything except this and that</strong>.</p>
  
  <p>So I have to add my own <em>RequestMappingHandlerMapping</em> to be able to specify the interceptor on it, and not globally. Because of <a href="https://stackoverflow.com/a/3231197/843699">this</a> and <a href="http://forum.springsource.org/showthread.php?81238-Conflict-between-lt-mvc-annotation-driven-gt-and-DefaultAnnotationHandlerMapping" rel="nofollow noreferrer">this</a>, it seems I can't simply define my own <em>RequestMappingHandlerMapping</em> while keeping the <em>&lt;mvc:annotation-driven /></em> element!</p>
  
  <p>So... With <a href="https://stackoverflow.com/questions/3693397/howto-get-rid-of-mvcannotation-driven">some help</a>, I've been able to get rid of the <em>&lt;mvc:annotation-driven /></em> element and pretty much everything works well now. I have my interceptor applied on everything but my resources. Everything works well, <strong>except the flash scope</strong>! </p>
  
  <pre><code>@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&lt;String, ?&gt; map = RequestContextUtils.getInputFlashMap(request); // map is NULL
      System.out.println(model.containsAttribute("myKey")); // Prints FALSE
  }
  </code></pre>
  
  <p>The flash map is NULL and my model doesn't contain my variable. When I try with the <em>&lt;mvc:annotation-driven /></em> element it works well! So my question is: what is missing from my context to make the flash scope work? </p>
  
  <p>I also did try to set "org.springframework.web" to a DEBUG logging level, and after the redirect there is nothing logged related to a FlashMap or FlashMapManager. It seems some required bean is definitely missing.</p>
  
  <p>Here are the interesting parts of my context file:</p>
  
  <pre><code>&lt;!-- commented! --&gt;
  &lt;!-- &lt;mvc:annotation-driven /&gt; --&gt;
  
  &lt;bean id="baseInterceptor" class="com.my.Interceptor" /&gt;
  
  &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"&gt;
      &lt;property name="order" value="0" /&gt;
      &lt;property name="interceptors"&gt;
          &lt;list&gt;
              &lt;ref bean="conversionServiceExposingInterceptor" /&gt;
              &lt;ref bean="baseInterceptor" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="myRequestMappingHandlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"&gt;
      &lt;property name="webBindingInitializer"&gt;
          &lt;bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"&gt;
              &lt;property name="conversionService" ref="conversionService" /&gt;
              &lt;property name="validator" ref="validator" /&gt;
          &lt;/bean&gt; 
      &lt;/property&gt;
      &lt;property name="messageConverters"&gt;
          &lt;list&gt;
              &lt;bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /&gt;
              &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter" /&gt;
              &lt;bean class="org.springframework.http.converter.FormHttpMessageConverter" /&gt;
              &lt;bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /&gt;
          &lt;/list&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /&gt;
  &lt;bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /&gt;
  &lt;bean id="conversionServiceExposingInterceptor" class="org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor"&gt;
      &lt;constructor-arg ref="conversionService" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"&gt;
      &lt;property name="basename" value="classpath:/messages/messages" /&gt;
      &lt;property name="defaultEncoding" value="UTF-8" /&gt;
  &lt;/bean&gt;
  
  &lt;bean class="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver"&gt;&lt;/bean&gt;
  
  &lt;mvc:resources order="-10" mapping="/public/**" location="/public/" /&gt;
  &lt;mvc:resources order="-10" mapping="/favicon.ico" location="/public/favicon.ico" /&gt;
  &lt;mvc:resources order="-10" mapping="/robots.txt" location="/public/robots.txt" /&gt;
  &lt;mvc:resources order="-10" mapping="/humans.txt" location="/public/humans.txt" /&gt;
  </code></pre>
  
  <p>What is missing for the flash scope to work?</p>
  
  <p><strong>UPDATE</strong> : See my answer for the solution... Nothing was missing actually. Only the Session was not working correctly and I found a way to make it work.</p>