Spring MVC 教程,快速入門,深入分析——轉發與重定向

  十四、轉發與重定向

可以通過redirect/forward:url方式轉到另一個Action進行連續的處理。

可以通過redirect:url 防止表單重複提交 

寫法如下:

return "forward:/order/add";

return "redirect:/index.jsp";

轉載請註明出處:原文地址:http://elf8848.iteye.com/blog/875830

 

 

帶參數重定向--RedirectAttributes

用戶保存或修改後,爲了防止用戶刷新瀏覽器(F5)導致表單重複提交,一般在保存或修改操作之後會redirect到一個結果頁面(不是forward),同時攜帶參數,如操作成功的提示信息。因爲是Redirect,Request裏的attribute不會傳遞過去。Spring在3.1才提供了這個能力--RedirectAttributes。 反覆按F5,操作成功的提示信息也不會再次出來(總共只出現一次),效果很理想。

 

Java代碼  收藏代碼

  1. public String save(@ModelAttribute("group") Group group, RedirectAttributes redirectAttributes) {  

  2.     accountManager.saveGroup(group);  

  3.     redirectAttributes.addFlashAttribute("message""操作成功");  

  4.     return "redirect:/account/group/";  

  5. }  

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章