SpirngMVC返回邏輯視圖名 可以分下面幾種情況:
1. servlet進行請求轉發,返回到jsp頁面,如 return "index.jsp" ;
2. servlet 返回結果,讓請求 重定向到某個jsp頁面 ,此時servlet 返回語句類似: return " redirect : index.jsp ";
3. servlet 的返回結果是 請求另外一個servlet 此時servlet 返回語句類似: return " redirect : goIndex.do ";
4. servlet 的返回結果是 請求另外一個servlet,並且還需要帶上請求參數,也就是Controller 間的帶參數重定向 ,此時servlet 返回語句就不能直接寫了,需要進行url拼接再返回:
類似:String url="redirect: goSeeComment.do?newsId="+newsid; return url;
舉個例子 ,下面的代碼片段實現這么一個功能,從一個新聞顯示列表中,點擊添加評論按鈕(跳轉到goAddComment.do,把新聞newsId 轉發到addComment.jsp),為該條新聞添加評論,添加完評論進行表單的提交(提交到addComment.do),然后再返回到這條新聞對應的評論列表:
1 //添加評論步驟1:獲取newsId,轉發到addComment.jsp
2 @RequestMapping("goAddComment.do") 3 public String goAddComment(Model model,Integer newsId){ 4 model.addAttribute("newsId", newsId); 5 return "addComment.jsp";//把newsId發送到添加評論頁面,頁面添加評論,提交時,再把這個id一起提交到servlet,以便后面重定向時作為參數
6 } 7
8 //添加評論步驟2:表單數據提交;保存成功,重新請求"goSeeComment.do"顯示評論
9 @RequestMapping("addComment.do") 10 public String addComment(Model model,NewsComment comment){//NewsComment類中定義了newsId這個屬性 11 Integer newsid = comment.getNewsId(); 12 //System.out.println("newsid:"+newsid); 13 service.addComment(comment); 18 String url="redirect: goSeeComment.do?newsId="+newsid; 19 System.out.println("請求重定向url"+url); 20 //添加成功,返回到原來那條新聞對應的評論列表頁
21 return url;//需要前端頁面把newsId傳過來
22 }
其他一些介紹SpringMVC帶參數請求重定向的文章鏈接如下:
http://blog.csdn.net/u011851478/article/details/51872977
http://blog.csdn.net/jaryle/article/details/52263717
http://blog.51cto.com/983836259/1877188
以下轉自:http://zwdsmileface.iteye.com/blog/2200813
spring MVC框架controller間跳轉,需重定向。有幾種情況:不帶參數跳轉,帶參數拼接url形式跳轉,帶參數不拼接參數跳轉,頁面也能顯示。
首先先來介紹一下不帶參數的重定向:
我在后台一個controller跳轉到另一個controller,為什么有這種需求呢,是這樣的。我有一個列表頁面,然后我會進行新增操作,新增在后台完成之后我要跳轉到列表頁面,不需要傳遞參數,列表頁面默認查詢所有的。
方式一:使用ModelAndView(這是Spring 2.0的時候所用到的方法)
return new ModelAndView("redirect:/toList");
這樣可以重定向到toList這個方法
方式二:返回String
return "redirect:/ toList ";
然后在說一下帶參數的重定向
第二種情況,列表頁面有查詢條件,跳轉后我的查詢條件不能丟掉,這樣就需要帶參數的了,帶參數可以拼接url
方式一:自己手動拼接url
new ModelAndView("redirect:/toList?param1="+value1+"¶m2="+value2);
這樣有個弊端,就是傳中文可能會有亂碼問題。
方式二:用RedirectAttributes,這個是發現的一個比較好用的一個類
這里用它的addAttribute方法,這個實際上重定向過去以后你看url,是它自動給你拼了你的url。
使用方法:
public String test(RedirectAttributes attributes)
{
attributes.addAttribute("test", "hello");
return "redirect:/test/test2";
}
這樣在toController這個方法中就可以通過獲得參數的方式獲得這個參數,再傳遞到頁面。過去的url還是和方式一一樣的。如果你細心的看重定向之后的url地址的話,你就會發現其實和上面的地址是一樣的,這樣也會出現上面那個方法出現的問題。
重點來了,咱們介紹一個不會出現中文亂碼,而且不會在你的Url上出現你所要傳遞的數據的,這樣就可以保證你在傳遞數據的安全
public String red(RedirectAttributes attributes)
{
attributes.addFlashAttribute("test", "hello");
return "redirect:/test/test2";
}
咱們用上面的方法進行數據傳遞你就會發現不會再Url上出現你要傳遞的數據,那么數據放到哪里去了,我們就來看看這是Spring 3.0新出現的特性,attributes.addFlashAttribute("test", "hello")實際存儲的屬性在flashmap,那么flashmap又是什么呢?
Flash 屬性 和 RedirectAttribute:通過FlashMap存儲一個請求的輸出,當進入另一個請求時作為該請求的輸入,典型場景如重定向(POST-REDIRECT-GET模式,1、POST時將下一次需要的數據放在FlashMap;2、重定向;3、通過GET訪問重定向的地址,此時FlashMap會把1放到FlashMap的數據取出放到請求中,並從FlashMap中刪除;從而支持在兩次請求之間保存數據並防止了重復表單提交)。
Spring Web MVC提供FlashMapManager用於管理FlashMap,默認使用SessionFlashMapManager,即數據默認存儲在session中
既然知道了怎么回事,那么我們就可以把它提取出來,怎么提取呢,很多人會說,既然存在session中,那就從session中獲取,結果發現沒有,那怎么辦?
下面我就給大家提供兩個方法讓大家把addFlashAttribute中的數據提取出來
方法一:利用httpServletRequest
public String test2(HttpServletRequest request)
{
Map<String,?> map = RequestContextUtils.getInputFlashMap(request);
System.out.println(map.get("test").toString());
return "/test/hello";
}
方法二:利用Spring提供的標簽@ModelAttribute
public String test2(@ModelAttribute("test") String str)
{
System.out.println(str);
return "/test/hello";
}
以上兩種方法是在后台Controller層獲取值的方法,如果是在前台頁面的話,就會比較簡單,直接利用el表達式就可以取到數據