<1> 不帶參數的重定向
方式一:使用ModelAndView
return new ModelAndView("redirect:/toList");
方式二:返回String
return "redirect:/ toList";
<2> 帶參數的重定向
方式一:自己手動
return new ModelAndView("redirect:/toList?param1="+value1+"¶m2="+value2);
弊端:傳中文可能亂碼
方式二:用RedirectAttributes類
使用addAttribute方法,自動給你拼接url
使用方法:
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr){
...
attr.addAttribute("param", value);
return "redirect:/toList";
}
在toList方法中可以通過獲得參數的方式獲取參數
2、請求轉發:// 轉發到toList請求
<1> 不帶參數的轉發
方式一:使用ModelAndView
return new ModelAndView("forward:/toList");
方式二:返回String
return "forward:/toList";
<2> 帶參數的轉發
方式一:使用ModelAndView
return new ModelAndView("forward:/toList?param1="+value1+"¶m2="+value2");
方式二:返回String
return "forward:/ toList?param1="+value1+"¶m2="+value2";