Spring MVC 重定向傳遞數(shù)據(jù)
1. 前言
本節(jié)課程和大家一起講解 Spring MVC 框架是如何跨請(qǐng)求傳遞數(shù)據(jù)的,讓數(shù)據(jù)在重定向期間能被讀出來(lái)。你需要重點(diǎn)掌握對(duì) RedirectAttributes 組件的使用。
2. 查詢(xún)字符串
如果是轉(zhuǎn)發(fā),數(shù)據(jù)模型只需要是請(qǐng)求作用域級(jí)別的,視圖解析器便能從數(shù)據(jù)模型中拿到所需要的數(shù)據(jù)。對(duì)于重定向,因?yàn)榭缌苏?qǐng)求,視圖無(wú)法讀出請(qǐng)求作用域級(jí)別的數(shù)據(jù)模型中的數(shù)據(jù)。
如果要讓數(shù)據(jù)模型中的數(shù)據(jù)被視圖識(shí)別出來(lái),則需要提升數(shù)據(jù)模型的作用域,如升級(jí)為會(huì)話(huà)作用域級(jí)別。所謂會(huì)話(huà)作用域,意味著數(shù)據(jù)會(huì)存放在服務(wù)器的會(huì)話(huà)緩存區(qū)。如果數(shù)據(jù)使用的頻率不是很高,會(huì)產(chǎn)生空間上的浪費(fèi)。
有沒(méi)有一種較佳的方案,即不浪費(fèi)服務(wù)器空間,又能傳遞數(shù)據(jù)了?
答案是肯定的。最原始的方式便是采用查詢(xún)字符串的方式。
如下面的實(shí)例:
@RequestMapping("/response03")
public String response03(ModelMap model) throws IOException {
// 發(fā)送給客戶(hù)端的響應(yīng)數(shù)據(jù)
String hello = "Hello";
model.addAttribute("data", hello);
return "redirect:/hello.jsp";
}
model 中的數(shù)據(jù)只是請(qǐng)求作用域級(jí)別,重定向后的 hello.jsp 中無(wú)法獲取此數(shù)據(jù), Spring MVC 內(nèi)部處理方式是把數(shù)據(jù)附加在 hello.jsp 后面。
打開(kāi)瀏覽器,輸入請(qǐng)求 http://localhost:8888/sm-demo/response03 地址,查看瀏覽器的地址欄中的 URL 變成 http://localhost:8888/sm-demo/hello.jsp?data=Hello。
Tips:數(shù)據(jù)附加在 URL 后面是 Spring MVC 自動(dòng)完成的。
把 hello.jsp 頁(yè)面中的數(shù)據(jù)讀取方式改成下面的方式(從查詢(xún)參數(shù)中讀?。?/p>
<div style="color:red">${param.data} </div>
這種方式有 2 個(gè)缺點(diǎn):
-
安全性低,數(shù)據(jù)赤裸裸地暴露在地址欄中;
-
如果數(shù)據(jù)量多,則整個(gè) URL 變得臃腫不易維護(hù)。
多數(shù)據(jù)實(shí)例:
@RequestMapping("/response03")
public String response03(ModelMap model) throws IOException {
// 發(fā)送給客戶(hù)端的響應(yīng)數(shù)據(jù)
String hello = "Hello";
model.addAttribute("data", hello);
model.addAttribute("id", 1);
return "redirect:/hello.jsp";
}
當(dāng)請(qǐng)求后,URL 會(huì)變成 http://localhost:8888/sm-demo/hello.jsp?data=Hello&id=1 。
3. 模板方式
Spring MVC 提供有一種所謂的模板方法,和前面的以查詢(xún)字符串方法進(jìn)行附加沒(méi)有多大區(qū)別。如下面的代碼,數(shù)據(jù)模型中的 data 對(duì)應(yīng)數(shù)據(jù)會(huì)以 URL 變量方式傳遞。數(shù)據(jù)模型中其它數(shù)據(jù)則以查詢(xún)字符串方式進(jìn)行傳遞。
@RequestMapping("/response04")
public String response04(ModelMap model) throws IOException {
// 發(fā)送給客戶(hù)端的響應(yīng)數(shù)據(jù)
String hello = "Hello";
model.addAttribute("data", hello);
model.addAttribute("id", 1);
return "redirect:/test/{data}";
}
?
@RequestMapping("/test/{data}")
public String response05(@PathVariable("data") String data,@RequestParam("id") int id)
throws IOException {
System.out.println(data);
System.out.println(id);
return null;
}
當(dāng)在瀏覽器中請(qǐng)求 http://localhost:8888/sm-demo/response04 后,瀏覽器的地址欄中會(huì)變成 :http://localhost:8888/sm-demo/test/Hello?id=1。
模板方式其本質(zhì)和查詢(xún)字符串沒(méi)有太多區(qū)別。
4. 使用 Flash 屬性
Spring MVC 使用重定向時(shí),內(nèi)部會(huì)做些處理,把數(shù)據(jù)以查詢(xún)字符串或變量參數(shù)方式進(jìn)行傳遞。對(duì)于簡(jiǎn)單的數(shù)據(jù)傳遞已經(jīng)足夠了。如果需要傳遞一個(gè)對(duì)象,則需要把一個(gè)對(duì)象拆分后再傳遞,顯然不是一種最佳方案。
Spring MVC 提供有 RedirectAttributes 組件,專(zhuān)用于重定向期間進(jìn)行數(shù)據(jù)傳遞,其本質(zhì)是把數(shù)據(jù)先保存在會(huì)話(huà)作用域中,但不會(huì)長(zhǎng)時(shí)間占據(jù)會(huì)話(huà)存儲(chǔ)空間,當(dāng)整個(gè)重定向結(jié)束后數(shù)據(jù)會(huì)自動(dòng)從會(huì)話(huà)作用域中刪除。
所以,RedirectAttributes 組件中的屬性也叫 Flash 屬性。
如下面實(shí)例:用戶(hù)對(duì)象以 Flash 屬性的方式封裝在模型對(duì)象中進(jìn)行傳遞。
@RequestMapping("/response05")
public String response06(RedirectAttributes model) throws IOException {
User user=new User("mk", "123");
model.addFlashAttribute("user", user);
return "redirect:/test01";
}
Tips:重定向路徑 test01 是控制器中的另一個(gè)方法,如下所示。
@RequestMapping("/test01")
public String response07(ModelMap model)
throws IOException {
User user= (User) model.get("user");
System.out.println(user);
return "hello";
}
自然,在 hello 視圖中很便捷的就能得到數(shù)據(jù)。
<body>
<div style="color:red"> ${user.userName} </div>
<div style="color:red"> ${user.userPassword} </div>
</body>
4. 小結(jié)
本章節(jié)和大家一起講解 Spring MVC 框架的重定向過(guò)程中是如何實(shí)現(xiàn)數(shù)據(jù)傳遞的,傳遞方式歸納起來(lái)就 2 種:
- 通過(guò) URL 附加數(shù)據(jù)的方式傳遞數(shù)據(jù);
- 使用 Spring MVC 提供的 RedirectAttributes 組件實(shí)現(xiàn)數(shù)據(jù)的傳遞。
兩種方式各有屬于自己的應(yīng)用場(chǎng)景,數(shù)據(jù)量不多時(shí)可以使用第一種方案。數(shù)據(jù)以對(duì)象方式進(jìn)行傳遞時(shí)可使用第二種方案。