转自:
下文笔者将讲述Spring MVC进行重定向的三种方法简介说明,如下所示:
response.sendRedirect重定向跳转
@RequestMapping(value="/testredirect",method = { RequestMethod.POST, RequestMethod.GET })
public ModelAndView testredirect(HttpServletResponse response){
response.sendRedirect("/index");
return null;
}
@RequestMapping(value="/testredirect",method = { RequestMethod.POST, RequestMethod.GET })
public String testredirect(HttpServletResponse response){
return "redirect:/index";
}
@RequestMapping("/testredirect")
public String testredirect(Model model, RedirectAttributes attr) {
attr.addAttribute("test", "java265");//跳转地址带上test参数
return "redirect:/user/users";
}
@RequestMapping(value="/restredirect",method = { RequestMethod.POST, RequestMethod.GET })
public ModelAndView restredirect(String userName){
ModelAndView model = new ModelAndView("redirect:/main/index");
return model;
}
带参数
@RequestMapping(value="/toredirect",method = { RequestMethod.POST, RequestMethod.GET })
public ModelAndView toredirect(String userName){
ModelAndView model = new ModelAndView("/main/index");
model.addObject("userName", userName); //把userName参数带入到controller的RedirectAttributes
return model;
}