基于springboot redirect重定向路徑問題總結(jié)
SpringMVC重定向視圖RedirectView小分析
前言
SpringMVC是目前主流的Web MVC框架之一。
本文所講的部分內(nèi)容跟SpringMVC的視圖機(jī)制有關(guān),SpringMVC的視圖機(jī)制請參考樓主的另一篇博客:
RedirectView這個(gè)視圖是跟重定向相關(guān)的,也是重定向問題的核心,我們來看看這個(gè)類的源碼。
路徑構(gòu)造完畢之后使用reponse進(jìn)行sendRedirect操作。
實(shí)例講解
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” , 那么會(huì)使用重定向。
SpringMVC找視圖名”redirect:index”的時(shí)候,本文使用的ViewResolver是FreeMarkerViewResolver。
FreeMarkerViewResolver解析視圖名的話,最調(diào)用父類之一的UrlBasedViewResolver中的createView方法。
通過構(gòu)造方法發(fā)現(xiàn),這個(gè)RedirectView使用相對路徑,兼容Http1.0,不暴露路徑變量。
test1方法:
- 進(jìn)入的路徑: /SpringMVCDemo/redirect/test1。 RedirectView使用相對路徑,那么重定向的路徑: /SpringMVCDemo/redirect/index
我們通過firebug看下路徑:
nice,驗(yàn)證了我們的想法。
test2方法同理:
- 進(jìn)入的路徑: /SpringMVCDemo/redirect/test2。 重定向的路徑: /SpringMVCDemo/redirect/login。
test3方法:
- 以 “/” 開頭并且使用相對路徑,那么會(huì)默認(rèn)加上contextPath。 進(jìn)入的路徑: /SpringMVCDemo/redirect/test3。 重定向的路徑: /SpringMVCDemo/index。
test4方法:
- 不使用默認(rèn)路徑,createTargetUrl方法中直接append “/index”。進(jìn)入的路徑: /SpringMVCDemo/redirect/test4。 重定向的路徑: /index。
test5方法:
- 不使用默認(rèn)路徑,createTargetUrl方法中直接append “index”。進(jìn)入的路徑: /SpringMVCDemo/redirect/test5。 重定向的路徑: /SpringMVCDemo/redirect/index。
- 其實(shí)這里有點(diǎn)意外,剛開始看的時(shí)候以為不使用絕對路徑,以后路徑會(huì)是/SpringMVCDemo/index或/index。 結(jié)果居然不是這樣,感覺SpringMVC這個(gè)取名有點(diǎn)尷尬,有點(diǎn)蛋疼。 其實(shí)RedirectView中的createTargetUrl方法就明白了,源碼是最好的文檔 0 0.
test6方法:
- 使用默認(rèn)路徑,該方法還使用了路徑變量。本文之前分析的時(shí)候說了RedirectView中構(gòu)造重定向路徑的時(shí)候會(huì)對路徑變量進(jìn)行替代,還會(huì)暴露model中的屬性,且這2個(gè)暴露分別受屬性expandUriTemplateVariables、exposeModelAttributes影響,這2個(gè)屬性默認(rèn)都是true。進(jìn)入的路徑: /SpringMVCDemo/redirect/test6/1。 重定向的路徑: /SpringMVCDemo/index1?test=test。
test7方法:
- 跟test6方法一樣,只不過我們不暴露model屬性,不替代路徑變量了。進(jìn)入的路徑: /SpringMVCDemo/redirect/test7/1。 重定向的路徑: /SpringMVCDemo/index{id}。
總結(jié)
簡單了分析了RedirectView視圖,并分析了該視圖的渲染源碼,并分析了重定向中的路徑問題,參數(shù)問題,路徑變量問題等常用的問題。
源碼真是最好的文檔,了解了視圖機(jī)制之后,再回過頭來看看RedirectView視圖,So easy。
最后感覺RedirectView的相對路徑屬性怪怪的,不使用相對路徑,”/” 開頭的直接就是服務(wù)器根路徑, 不帶 “/” 開頭的,又是相對路徑, 有點(diǎn)蛋疼。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springBoot熱部署、請求轉(zhuǎn)發(fā)與重定向步驟詳解
- springboot如何重定向外部網(wǎng)頁
- SpringBoot中處理的轉(zhuǎn)發(fā)與重定向方式
- springboot?實(shí)戰(zhàn):異常與重定向問題
- 使用springboot跳轉(zhuǎn)到指定頁面和(重定向,請求轉(zhuǎn)發(fā)的實(shí)例)
- springboot如何重定向攜帶數(shù)據(jù) RedirectAttributes
- springboot 重定向方式(redirect前綴)
- springboot項(xiàng)目攔截器重定向循環(huán)問題的解決
- springboot 如何重定向redirect 并隱藏參數(shù)
- Springboot轉(zhuǎn)發(fā)重定向?qū)崿F(xiàn)方式解析
- SpringBoot后端服務(wù)重定向的實(shí)現(xiàn)示例
相關(guān)文章
Java中HTTP GET方法調(diào)用帶有body的問題解決
這篇文章主要為大家詳細(xì)介紹了Java如何解決HTTP GET方法調(diào)用帶有body的問題,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2024-02-02
SpringMVC的組件之HandlerExceptionResolver詳解
這篇文章主要介紹了SpringMVC的組件之HandlerExceptionResolver詳解,不管是在處理請求映射(HandlerMapping),還是在請求被處理(Handler)時(shí)拋出的異常,DispatcherServlet都會(huì)委托給HandlerExceptionResolver進(jìn)行異常處理,該接口只有一個(gè)方法,需要的朋友可以參考下2023-10-10
QQ好友列表樹形列表java代碼實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了QQ好友列表樹形列表簡單實(shí)現(xiàn)方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08

