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

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

    Hello,

    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:
    Code:
    @Configuration
    @EnableWebMvc
    @PropertySource(name="appProperties", value="classpath:/app.properties")
    @ComponentScan(basePackages = { "com.company.intranet", }, includeFilters = {@ComponentScan.Filter(Controller.class)}, excludeFilters = @ComponentScan.Filter(Configuration.class), useDefaultFilters = false)
    public class WebConfig extends WebMvcConfigurerAdapter {
    
      @Inject
      protected TabInterceptor tabInterceptor;
    
    
      @Override
      public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tabInterceptor).addPathPatterns("/**");
      }
    }
    The interceptor:
    Code:
    @Named("tabInterceptor")
    public class TabInterceptor extends HandlerInterceptorAdapter {
    
      @Override
      public boolean preHandle(final HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return true;
      }
    
      @Override
      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:
    Code:
    @Controller
    public class HistoryController {
      
      @Inject
      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) {
          model.addAllAttributes(result.getModel());
        }
        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";
      }
    
      @RequestMapping("/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: https://jira.springsource.org/browse/SPR-8968
    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?
Working...
X