在Sping Boot项目中,使用JSP做view,可能会遇到new ModelAndView("redirect:/user")无效的情况。
处理方法:
1、 Try to use RedirectView explicite
@RequestMapping(value="/gotoNextPage",method = RequestMethod.GET)
public ModelAndView gotoNextPage(HttpServletRequest request, HttpServletResponse response){
System.out.println("Inside gotoNextPage!!!!!!");
ModelMap model = new ModelMap();
model.add("message", "next page");
return new ModelAndView(
new RedirectView("/nextpage", true),
//or new RedirectView("/nextpage.html", true),
model
);
}
2、I'd try to simplify the method to use Model and return a simple String
@RequestMapping(value="/gotoNextPage",method = RequestMethod.GET)
public String gotoNextPage(Model model){
LOG.debug("Inside gotoNextPage!!!!!!");
model.addAttribute("message", "next page");
return "redirect:nextpage.html";
}
试过第1种了。第2种没有做过试验。
原文:http://stackoverflow.com/questions/33327678/springboot-controller-redirect-not-working