spring mvc controller中重定向url的几种方式
- 使用ModelAndView
return new ModelAndView(“redirect:url”);
-
方法的返回值为 String
return “redirect:url”; -
在方法参数中定义HttpServletResponse response
response.sendRedirect(url);
以上方法中的参数可以放在以?p1=v1&p2=v2的形式来设置参数 -
在方法中定义RedirectAttributes attr参数
RedirectAttributes是SpringMVC3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的.
public String testPar(String id,RedirectAttributes attr){
//在实际跳转中,会自动将参数附加在请求中
attr.addAttribute("p1","pv1");
return "redirect:url";
}