Spring MVC 3.1 RedirectAttributes无效

我正在尝试在Spring MVC 3.1-Release中实现RedirectAttributesfunction

我正在发送简单的表单到Post URL,并希望看到我在重定向中发送的值:

我的控制器看起来像这样:

@Controller public class DefaultController { @RequestMapping(value="/index.html", method=RequestMethod.GET) public ModelAndView indexView(){ ModelAndView mv = new ModelAndView("index"); return mv; } @RequestMapping(value="/greetings.action", method=RequestMethod.POST) public ModelAndView startTask(@RequestParam("firstName") String firstName,RedirectAttributes redirectAttributes){ redirectAttributes.addFlashAttribute("redirectAttributes.firstName", firstName); ModelAndView mv = new ModelAndView(new RedirectView("success.html")); return mv; } @RequestMapping(value="/success.html", method=RequestMethod.GET) public ModelAndView successView(){ ModelAndView mv = new ModelAndView("success"); return mv; } } 

现在我的Servlet XML看起来像这样:

      /   .jsp    

我的问题是,在success.html视图中,redirectAttributes为空。

  
${redirectAttributes.firstName}

什么都不打印。

它不起作用,因为您使用ModelAndView作为返回值。

请参阅JIRA问题: https : //jira.springsource.org/browse/SPR-8968

看起来您需要删除“redirectAttributes”。 从关键名称“

代替:

  redirectAttributes.addFlashAttribute("redirectAttributes.firstName", firstName); 

做这个:

  redirectAttributes.addFlashAttribute("firstName", firstName);