SpringMVC重定向視圖RedirectView小分析
前言
SpringMVC是目前主流的Web MVC框架之一。
本文所講的部分內(nèi)容跟SpringMVC的視圖機制有關(guān),SpringMVC的視圖機制請參考樓主的另一篇博客:
RedirectView這個視圖是跟重定向相關(guān)的,也是重定向問題的核心,我們來看看這個類的源碼。
路徑構(gòu)造完畢之后使用reponse進行sendRedirect操作。
實例講解
Controller代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
@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; } |
1
2
3
4
5
6
7
8
9
|
@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解析視圖名的話,最調(diào)用父類之一的UrlBasedViewResolver中的createView方法。
通過構(gòu)造方法發(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。 結(jié)果居然不是這樣,感覺SpringMVC這個取名有點尷尬,有點蛋疼。 其實RedirectView中的createTargetUrl方法就明白了,源碼是最好的文檔 0 0.
test6方法:
- 使用默認路徑,該方法還使用了路徑變量。本文之前分析的時候說了RedirectView中構(gòu)造重定向路徑的時候會對路徑變量進行替代,還會暴露model中的屬性,且這2個暴露分別受屬性expandUriTemplateVariables、exposeModelAttributes影響,這2個屬性默認都是true。進入的路徑: /SpringMVCDemo/redirect/test6/1。 重定向的路徑: /SpringMVCDemo/index1?test=test。
test7方法:
- 跟test6方法一樣,只不過我們不暴露model屬性,不替代路徑變量了。進入的路徑: /SpringMVCDemo/redirect/test7/1。 重定向的路徑: /SpringMVCDemo/index{id}。
總結(jié)
簡單了分析了RedirectView視圖,并分析了該視圖的渲染源碼,并分析了重定向中的路徑問題,參數(shù)問題,路徑變量問題等常用的問題。
源碼真是最好的文檔,了解了視圖機制之后,再回過頭來看看RedirectView視圖,So easy。
最后感覺RedirectView的相對路徑屬性怪怪的,不使用相對路徑,”/” 開頭的直接就是服務(wù)器根路徑, 不帶 “/” 開頭的,又是相對路徑, 有點蛋疼。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_37992974/article/details/101455844