使用SpringMVC在redirect重定向的時(shí)候攜帶參數(shù)的問(wèn)題
在redirect重定向的時(shí)候攜帶參數(shù)問(wèn)題
SpringMVC 中常用到 redirect來(lái)實(shí)現(xiàn)重定向。但使用場(chǎng)景各有需求,如果只是簡(jiǎn)單的頁(yè)面跳轉(zhuǎn)顯然無(wú)法滿(mǎn)足所有要求,比如重定向時(shí)需要在 url 中拼接參數(shù),或者返回的頁(yè)面需要傳遞 Model。SpringMVC 用 RedirectAttributes 解決了這兩個(gè)需要。
首先,在 Controller 中做 redirect 中可用如下方式實(shí)現(xiàn):
return new ModelAndView(“redirect:/index”);
或
return “redirect:/index”;
此時(shí),如果只是重定向至某一 URL 或者比較簡(jiǎn)單地址,也可以不用 RedirectAttributes,直接拼接,如:return “redirect:/index?param1=value1″;
但是這樣似乎有點(diǎn)過(guò)于簡(jiǎn)單粗暴,而且參數(shù)多了很容易使代碼可讀性變差。使用 RedirectAttributes 來(lái)設(shè)置重定向頁(yè)面的參數(shù),SpringMVC 會(huì)自動(dòng)拼接 url。
接下來(lái)主要介紹該對(duì)象的兩個(gè)方法:
1. addAttribute
@RequestMapping("/save") public String save(User user, RedirectAttributes redirectAttributes) { ? ? redirectAttributes.addAttribute("param", "value1"); ? ? return "redirect:/index"; }
請(qǐng)求 /save 后,跳轉(zhuǎn)至/index,并且會(huì)在url拼接 ?param=value1。
2. addFlashAttribute
@RequestMapping("/save") public String save(User user, RedirectAttributes redirectAttributes) { ? ? redirectAttributes.addFlashAttribute("param", "value1"); ? ? return "redirect:/index"; }
請(qǐng)求 /save 后,跳轉(zhuǎn)至 /index,并且可以在 index 對(duì)應(yīng)的模版中通過(guò)表達(dá)式,比如 jsp 中 jstl 用 ${param},獲取返回值。該值其實(shí)是保存在 session 中的,并且會(huì)在下次重定向請(qǐng)求時(shí)刪除。
RedirectAttributes 中兩個(gè)方法的簡(jiǎn)單介紹就是這樣。
redirect重定向3種方式(帶參數(shù))
Spring MVC中做form表單功能提交時(shí),防止用戶(hù)客戶(hù)端后退或者刷新時(shí)重復(fù)提交問(wèn)題,需要在服務(wù)端進(jìn)行重定向跳轉(zhuǎn),其中redirect是直接跳轉(zhuǎn)到其他頁(yè)面,有以下3種方法進(jìn)行重定向。
redirect重定向流程
客戶(hù)發(fā)送一個(gè)請(qǐng)求到服務(wù)器,服務(wù)器匹配servlet,這都和請(qǐng)求轉(zhuǎn)發(fā)一樣,servlet處理完之后調(diào)用了sendRedirect()這個(gè)方法,這個(gè)方法是response的方法,所以,當(dāng)這個(gè)servlet處理完之后,看到response.senRedirect()方法,立即向客戶(hù)端返回這個(gè)響應(yīng),響應(yīng)行告訴客戶(hù)端你必須要再發(fā)送一個(gè)請(qǐng)求,去訪問(wèn)test.jsp,緊接著客戶(hù)端受到這個(gè)請(qǐng)求后,立刻發(fā)出一個(gè)新的請(qǐng)求,去請(qǐng)求test.jsp,這里兩個(gè)請(qǐng)求互不干擾,相互獨(dú)立,在前面request里面setAttribute()的任何東西,在后面的request里面都獲得不了??梢?jiàn),在sendRedirect()里面是兩個(gè)請(qǐng)求,兩個(gè)響應(yīng)。
1. response.sendRedirect重定向跳轉(zhuǎn)
@RequestMapping(value="/testredirect",method = { RequestMethod.POST, RequestMethod.GET }) ? public ModelAndView testredirect(HttpServletResponse response){ ? ? ? response.sendRedirect("/index"); ? ? return null;? }
2. ViewResolver直接跳轉(zhuǎn)
不帶參數(shù)
@RequestMapping(value="/testredirect",method = { RequestMethod.POST, RequestMethod.GET }) ? public ?String testredirect(HttpServletResponse response){ ? ? ? return "redirect:/index"; ? }?
帶參數(shù)
@RequestMapping("/testredirect") public String testredirect(Model model, RedirectAttributes attr) { ?? ?attr.addAttribute("test", "51gjie");//跳轉(zhuǎn)地址帶上test參數(shù) ? ? attr.addFlashAttribute("u2", "51gjie");//跳轉(zhuǎn)地址不帶上u2參數(shù) ?? ?return "redirect:/user/users"; }
使用RedirectAttributes的addAttribute方法傳遞參數(shù)會(huì)跟隨在URL后面,如上代碼即為http:/index.action?test=51gjie
使用addFlashAttribute不會(huì)跟隨在URL后面,會(huì)把該參數(shù)值暫時(shí)保存于session,待重定向url獲取該參數(shù)后從session中移除,這里的redirect必須是方法映射路徑,jsp無(wú)效。你會(huì)發(fā)現(xiàn)redirect后的jsp頁(yè)面中b只會(huì)出現(xiàn)一次,刷新后b再也不會(huì)出現(xiàn)了,這驗(yàn)證了上面說(shuō)的,b被訪問(wèn)后就會(huì)從session中移除。對(duì)于重復(fù)提交可以使用此來(lái)完成.
spring mvc設(shè)置下RequestMappingHandlerAdapter 的ignoreDefaultModelOnRedirect=true,這樣可以提高效率,避免不必要的檢索。
3. ModelAndView重定向
不帶參數(shù)
@RequestMapping(value="/restredirect",method = { RequestMethod.POST, RequestMethod.GET }) ? public ?ModelAndView restredirect(String userName){ ? ? ? ModelAndView ?model = new ModelAndView("redirect:/main/index"); ? ? ? ? return model; ? }
帶參數(shù)
@RequestMapping(value="/toredirect",method = { RequestMethod.POST, RequestMethod.GET }) ? public ?ModelAndView toredirect(String userName){ ? ? ? ModelAndView ?model = new ModelAndView("/main/index"); ?? ? ? model.addObject("userName", userName); ?//把userName參數(shù)帶入到controller的RedirectAttributes ? ? return model; ? }
小結(jié)一下:
1.redirect重定向可以跳轉(zhuǎn)到任意服務(wù)器,可以用在系統(tǒng)間的跳轉(zhuǎn)。
2.Spring MVC中redirect重定向,參數(shù)傳遞可以直接拼接url也可以使用RedirectAttributes來(lái)處理,由于是不同的請(qǐng)求,重定向傳遞的參數(shù)會(huì)在地址欄顯示,所以傳遞時(shí)要對(duì)中文編碼進(jìn)行處理。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringCloud讀取Nacos配置中心報(bào)錯(cuò)及遇到的坑:Could?not?resolve?placehold
這篇文章主要介紹了SpringCloud讀取Nacos配置中心報(bào)錯(cuò):Could?not?resolve?placeholder?‘xxx’?in?value?‘${xxx},本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03springboot整合vue實(shí)現(xiàn)上傳下載文件
這篇文章主要為大家詳細(xì)介紹了springboot整合vue實(shí)現(xiàn)上傳下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11SpringBoot+mybatis+Vue實(shí)現(xiàn)前后端分離項(xiàng)目的示例
本文主要介紹了SpringBoot+mybatis+Vue實(shí)現(xiàn)前后端分離項(xiàng)目的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12一文詳解SpringBoot中CommandLineRunner接口
Spring Boot的CommandLineRunner接口是一個(gè)函數(shù)式接口,用于在Spring Boot應(yīng)用程序啟動(dòng)后執(zhí)行一些初始化操作,它提供了一個(gè)run方法,該方法在應(yīng)用程序啟動(dòng)后被調(diào)用,本文給大家詳細(xì)介紹了SpringBoot中CommandLineRunner接口,需要的朋友可以參考下2023-10-10SpringBoot2整合Redis多數(shù)據(jù)源步驟詳解
這篇文章主要介紹了SpringBoot2整合Redis多數(shù)據(jù)源步驟詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Java的Comparable,Comparator和Cloneable三大接口詳解
這篇文章主要為大家詳細(xì)介紹了Java的Comparable,Comparator和Cloneable的接口,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03Java并發(fā)編程之CountDownLatch源碼解析
這篇文章主要介紹了Java并發(fā)編程之CountDownLatch源碼解析,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java并發(fā)編程的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04