SpringMVC 重新定向redirect請求中攜帶數(shù)據(jù)方式
SpringMVC 重新定向redirect請求中攜帶數(shù)據(jù)
- 在控制器方法返回的視圖名稱中使用了redirect:前綴,這時(shí)返回的String不是用來尋找視圖,而是瀏覽器進(jìn)行跳轉(zhuǎn)的路徑:
return “redirect:/spitter/” + spitter.getUsername();
當(dāng)一個(gè)控制器返回的是一個(gè)redirect時(shí),原來的request會終止,并且會開啟一個(gè)新的HTTP請求。原來request中所有的model數(shù)據(jù)都會清空。新的request不會有任何的model數(shù)據(jù),如圖:
現(xiàn)在不能再redirect時(shí)使用model來傳遞數(shù)據(jù)了。但是還有其他方法用來從重定向的方法中獲取數(shù)據(jù):
- 將數(shù)據(jù)轉(zhuǎn)換為路徑參數(shù)或者查詢參數(shù)
- 在flash屬性中發(fā)送數(shù)據(jù)首先來看一下Spring如何在路徑參數(shù)或者查詢參數(shù)中傳遞數(shù)據(jù)。
使用URL模版重定向
@RequestMapping(value="/register", method=POST) public String processRegistration(Spitter spitter, Model model) { spitterRepository.save(spitter); model.addAttribute("username", spitter.getUsername()); return "redirect:/spitter/{username}"; } @RequestMapping(value = "/{username}", method = RequestMethod.GET) public String showSpitterProfile(@PathVariable String username, Model model) { System.out.println("showSpitterProfile"); Spitter spitter = spittleRepository.findByUsername(username); model.addAttribute(spitter); return "Profile"; }
model中其他的原始值也會作為查詢參數(shù)添加到重定向URL中。例如,除了username,model同時(shí)也包括新建的Spitter對象的id屬性:
@RequestMapping(value="/register", method=POST) public String processRegistration(Spitter spitter, Model model) { spitterRepository.save(spitter); model.addAttribute("username", spitter.getUsername()); model.addAttribute("spitterId", spitter.getId()); return "redirect:/spitter/{username}"; }
但是由于model中的spitterId屬性并沒有映射到URL中的占位符,它會自動(dòng)作為查詢參數(shù)。
如果username是habuma,spitterId是42,那么返回的重定向路徑將是/spitter/habuma?spitterId=42。
使用flash屬性
Spring提供了通過RedirectAttributes來設(shè)置flash屬性,RedirectAttributes作為Model的子接口,新增了一些方法用來設(shè)置flash屬性。
@RequestMapping(value="/register", method=POST) public String processRegistration(Spitter spitter, RedirectAttributes model) { spitterRepository.save(spitter); model.addAttribute("username", spitter.getUsername()); model.addFlashAttribute("spitter", spitter); return "redirect:/spitter/{username}"; }
在重定向執(zhí)行之前,所有的flash屬性都會復(fù)制到會話中。在重定向后,存在會話中的flash屬性會被取出,并從會話轉(zhuǎn)移到模型之中。處理重定向的方法就能從模型中訪問Spitter對象了,就像獲取其他的模型對象一樣
@RequestMapping(value = "/{username}", method = RequestMethod.GET) public String showSpitterProfile(@PathVariable("username") String username, Model model) { if (!model.containsAttribute("spitter")) { Spitter spitter = spitterRepository.findByUsername(username); model.addAttribute(spitter); } return "profile"; }
showSpitterProfile()方法所做的第一件事就是檢查是否存有key為spitter的model屬性。如果模型中包含spitter屬性,那就什么都不用做了。這里面包含的Spitter對象將會傳遞到視圖中進(jìn)行渲染。但是如果模型中不包含spitter屬性的話,那么showSpitterProfile()將會從Repository中查找Spitter,并將其存放到模型中。
重新定向顯示數(shù)據(jù)
.... .... <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> .... .... username:<c:out value="${spitter.username}"/><br/> firstName:<c:out value="${spitter.firstName}"/> lastName: <c:out value="${spitter.lastName}"/><br/> email:<c:out value="${spitter.email}"/> ....
SpringMVC 幾種重定向攜帶數(shù)據(jù)方法
1.拼接字符串
return "redirect:/page/second?param1=lay¶m2=lay2";
2.采用RedirectAttribute來傳參數(shù)
public String first(RedirectAttribute redirectAttribute){ redirectAttribute.addAttribute("param1", "lay"); return "redirect:/page/second"; }
3.RedirectAttribute—–addFlashAttribute()的用法
@Controller @RequestMapping("/page") public class redirectDemo{ @RequestMapping("/first") public String first(RedirectAttribute redirectAttribute){ redirectAttribute.addFlashAttibute("param1", "lay"); return "redirect:/page/second"; } @RequestMapping("/second") public String second(@ModelAttribute("param1") String param1){ System.out.println(param1); return "second"; } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringMVC詳解如何映射請求數(shù)據(jù)
- Java超詳細(xì)講解SpringMVC如何獲取請求數(shù)據(jù)
- 關(guān)于SpringMVC請求域?qū)ο蟮臄?shù)據(jù)共享問題
- 使用springmvc的controller層獲取到請求的數(shù)據(jù)方式
- Springmvc獲取前臺請求數(shù)據(jù)過程解析
- Springmvc處理ajax請求并返回json數(shù)據(jù)
- SpringMVC 跨重定向請求傳遞數(shù)據(jù)的方法實(shí)現(xiàn)
- SpringMVC解析JSON請求數(shù)據(jù)問題解析
- SpringMVC請求數(shù)據(jù)詳解講解
相關(guān)文章
詳解SpringBoot?Start組件開發(fā)之記錄接口日志信息
這篇文章主要為大家介紹了SpringBoot-Start組件開發(fā)之記錄接口日志信息詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Java實(shí)現(xiàn)對一行英文進(jìn)行單詞提取功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)對一行英文進(jìn)行單詞提取功能,結(jié)合實(shí)例形式分析了java基于StringTokenizer類進(jìn)行字符串分割的相關(guān)操作技巧,需要的朋友可以參考下2017-10-10在已有spring的基礎(chǔ)上集成hibernate的實(shí)例講解
下面小編就為大家?guī)硪黄谝延衧pring的基礎(chǔ)上集成hibernate的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過程圖解
這篇文章主要介紹了Java上傳文件到服務(wù)器指定文件夾實(shí)現(xiàn)過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08java為移動(dòng)端寫接口開發(fā)實(shí)例
本篇文章主要介紹了java如何為移動(dòng)端寫接口,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08深入理解Spring MVC的數(shù)據(jù)轉(zhuǎn)換
這篇文章主要給大家介紹了關(guān)于Spring MVC數(shù)據(jù)轉(zhuǎn)換的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。2017-09-09