基于springboot redirect重定向路徑問題總結
SpringMVC重定向視圖RedirectView小分析
前言
SpringMVC是目前主流的Web MVC框架之一。
本文所講的部分內容跟SpringMVC的視圖機制有關,SpringMVC的視圖機制請參考樓主的另一篇博客:
RedirectView這個視圖是跟重定向相關的,也是重定向問題的核心,我們來看看這個類的源碼。
路徑構造完畢之后使用reponse進行sendRedirect操作。
實例講解
Controller代碼
@Controller @RequestMapping(value = “/redirect”) public class TestRedirectController { @RequestMapping("/test1") public ModelAndView test1() { view.setViewName("redirect:index"); return view; } @RequestMapping("/test2") public ModelAndView test2() { view.setViewName("redirect:login"); return view; } @RequestMapping("/test3") public ModelAndView test3(ModelAndView view) { view.setViewName("redirect:/index"); return view; } @RequestMapping("/test4") public ModelAndView test4(ModelAndView view) { view.setView(new RedirectView("/index", false)); return view; } @RequestMapping("/test5") public ModelAndView test5(ModelAndView view) { view.setView(new RedirectView("index", false)); return view; } @RequestMapping("/test6/{id}") public ModelAndView test6(ModelAndView view, @PathVariable("id") int id) { view.setViewName("redirect:/index{id}"); view.addObject(“test”, “test”); return view; }
@RequestMapping("/test7/{id}") public ModelAndView test7(ModelAndView view, @PathVariable("id") int id) { RedirectView redirectView = new RedirectView("/index{id}"); redirectView.setExpandUriTemplateVariables(false); redirectView.setExposeModelAttributes(false); view.setView(redirectView); view.addObject("test", "test"); return view; }
先看test1方法,返回值 “redirect:index” , 那么會使用重定向。
SpringMVC找視圖名”redirect:index”的時候,本文使用的ViewResolver是FreeMarkerViewResolver。
FreeMarkerViewResolver解析視圖名的話,最調用父類之一的UrlBasedViewResolver中的createView方法。
通過構造方法發(fā)現(xiàn),這個RedirectView使用相對路徑,兼容Http1.0,不暴露路徑變量。
test1方法:
- 進入的路徑: /SpringMVCDemo/redirect/test1。 RedirectView使用相對路徑,那么重定向的路徑: /SpringMVCDemo/redirect/index
我們通過firebug看下路徑:
nice,驗證了我們的想法。
test2方法同理:
- 進入的路徑: /SpringMVCDemo/redirect/test2。 重定向的路徑: /SpringMVCDemo/redirect/login。
test3方法:
- 以 “/” 開頭并且使用相對路徑,那么會默認加上contextPath。 進入的路徑: /SpringMVCDemo/redirect/test3。 重定向的路徑: /SpringMVCDemo/index。
test4方法:
- 不使用默認路徑,createTargetUrl方法中直接append “/index”。進入的路徑: /SpringMVCDemo/redirect/test4。 重定向的路徑: /index。
test5方法:
- 不使用默認路徑,createTargetUrl方法中直接append “index”。進入的路徑: /SpringMVCDemo/redirect/test5。 重定向的路徑: /SpringMVCDemo/redirect/index。
- 其實這里有點意外,剛開始看的時候以為不使用絕對路徑,以后路徑會是/SpringMVCDemo/index或/index。 結果居然不是這樣,感覺SpringMVC這個取名有點尷尬,有點蛋疼。 其實RedirectView中的createTargetUrl方法就明白了,源碼是最好的文檔 0 0.
test6方法:
- 使用默認路徑,該方法還使用了路徑變量。本文之前分析的時候說了RedirectView中構造重定向路徑的時候會對路徑變量進行替代,還會暴露model中的屬性,且這2個暴露分別受屬性expandUriTemplateVariables、exposeModelAttributes影響,這2個屬性默認都是true。進入的路徑: /SpringMVCDemo/redirect/test6/1。 重定向的路徑: /SpringMVCDemo/index1?test=test。
test7方法:
- 跟test6方法一樣,只不過我們不暴露model屬性,不替代路徑變量了。進入的路徑: /SpringMVCDemo/redirect/test7/1。 重定向的路徑: /SpringMVCDemo/index{id}。
總結
簡單了分析了RedirectView視圖,并分析了該視圖的渲染源碼,并分析了重定向中的路徑問題,參數(shù)問題,路徑變量問題等常用的問題。
源碼真是最好的文檔,了解了視圖機制之后,再回過頭來看看RedirectView視圖,So easy。
最后感覺RedirectView的相對路徑屬性怪怪的,不使用相對路徑,”/” 開頭的直接就是服務器根路徑, 不帶 “/” 開頭的,又是相對路徑, 有點蛋疼。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
- springBoot熱部署、請求轉發(fā)與重定向步驟詳解
- springboot如何重定向外部網(wǎng)頁
- SpringBoot中處理的轉發(fā)與重定向方式
- springboot?實戰(zhàn):異常與重定向問題
- 使用springboot跳轉到指定頁面和(重定向,請求轉發(fā)的實例)
- springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
- springboot 重定向方式(redirect前綴)
- springboot項目攔截器重定向循環(huán)問題的解決
- springboot 如何重定向redirect 并隱藏參數(shù)
- Springboot轉發(fā)重定向實現(xiàn)方式解析
- SpringBoot后端服務重定向的實現(xiàn)示例
相關文章
Mockito mock Kotlin Object類方法報錯解決方法
這篇文章主要介紹了Mockito mock Kotlin Object類方法報錯解決方法,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-09-09Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗功能
這篇文章主要介紹了Spring Cloud Gateway 使用JWT工具類做用戶登錄校驗的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01java WSDL接口webService實現(xiàn)方式
這篇文章主要為大家詳細介紹了java WSDL接口webService實現(xiàn)方式的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04解決Java執(zhí)行Cmd命令出現(xiàn)的死鎖問題
這篇文章主要介紹了關于Java執(zhí)行Cmd命令出現(xiàn)的死鎖問題解決,解決方法就是在waitfor()方法之前讀出窗口的標準輸出、輸出、錯誤緩沖區(qū)中的內容,本文給大家介紹的非常詳細,需要的朋友可以參考下2022-07-07