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方法。
通過構造方法發現,這個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視圖,并分析了該視圖的渲染源碼,并分析了重定向中的路徑問題,參數問題,路徑變量問題等常用的問題。
源碼真是最好的文檔,了解了視圖機制之後,再回過頭來看看RedirectView視圖,So easy。
最後感覺RedirectView的相對路徑屬性怪怪的,不使用相對路徑,”/” 開頭的直接就是伺服器根路徑, 不帶 “/” 開頭的,又是相對路徑, 有點蛋疼。
希望本文能夠幫助讀者了解SpringMVC的重定向相關問題