Announcement Announcement Module
No announcement yet.
Flash attributes stop working with use of interceptor ModelAndView.addObject Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Flash attributes stop working with use of interceptor ModelAndView.addObject


    I am currently in the process of upgrading some projects to use Spring 3.1, but have run into an issue with one of our apps when trying to utilize the new flash attributes. When adding attributes to the ModelAndView object in an interceptor, flash attributes stop working. If no attributes are added, however, the flash attributes work fine. Here is a skimmed down example:

    The WebConfig:
    @PropertySource(name="appProperties", value="classpath:/")
    @ComponentScan(basePackages = { "", }, includeFilters = {@ComponentScan.Filter(Controller.class)}, excludeFilters = @ComponentScan.Filter(Configuration.class), useDefaultFilters = false)
    public class WebConfig extends WebMvcConfigurerAdapter {
      protected TabInterceptor tabInterceptor;
      public void addInterceptors(InterceptorRegistry registry) {
    The interceptor:
    public class TabInterceptor extends HandlerInterceptorAdapter {
      public boolean preHandle(final HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
      public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
          throws Exception {
        System.out.println("\nTab Interceptor History Search Errors: " + modelAndView.getModelMap().get("historySearchErrors") + "\n");
        System.out.println("\nTab Interceptor History Search Criteria: " + modelAndView.getModelMap().get("historySearchCriteria") + "\n");
        if (modelAndView != null) {
          Object object = new Object(); //some object that needs to be added to all views and controllers via an interceptor.
          modelAndView.addObject("message", object); //comment this line out and flash attributes start working again.
    The Controller:
    public class HistoryController {
      protected HistoryMapper historyMapper;
      @RequestMapping(value = "/history", method = RequestMethod.GET)
      public String history(ModelMap model, RedirectAttributes redirectAttributes) {
        BindingResult result = (BindingResult) model.get("historySearchErrors");
        System.out.println("Binding Result: " + result);  //prints out "null"
        System.out.println("redirectAttributes: " + redirectAttributes.getFlashAttributes());  //prints out empty map
        if (result != null) {
        else {
          model.addAttribute("historySearchCriteria", new HistorySearchCriteria());
        return "historyform";
      @RequestMapping(value = "/history", method = RequestMethod.POST)
      public String search(@Valid HistorySearchCriteria historySearchCriteria, BindingResult bindingResult,
          RedirectAttributes redirectAttributes, ModelMap model) {
        System.out.println("HistorySearchCriteria: " + historySearchCriteria);
        if (bindingResult.hasErrors()) {
          redirectAttributes.addFlashAttribute("historySearchErrors", bindingResult);
          System.out.println("redirectAttributes: " + redirectAttributes.getFlashAttributes()); //has the flash attribute in the map
          return "redirect:/history";
        redirectAttributes.addFlashAttribute("historySearchCriteria", historySearchCriteria);
        return "redirect:/historyresults";
      public String historyResults(RedirectAttributes redirectAttributes, ModelMap model) {
        HistorySearchCriteria hsc = (HistorySearchCriteria) model.get("historySearchCriteria");
        System.out.println("History Search Criteria: " + hsc); //prints out "null"
        System.out.println("redirectAttributes: " + redirectAttributes.getFlashAttributes()); //prints out as an empty map
        //Do actual search here and add results to the model for jsp to display.
        List<HistoryResult> results = historyMapper.getResults(hsc);
        model.put("results", results);
        return "historyresults";
    This may or may not be related to this JIRA ticket:
    I have tried updating my version to Spring 3.1.1, but no change.

    For now I can just put the object into the session and remove it manually as a workaround, but would like to get it working using the Spring Flash attributes instead. Not sure if this would be considered a spring bug or more of a feature request? Also, is there a better work around for this?