SpringMVC重定向視圖RedirectView小分析


目錄

前言

SpringMVC是目前主流的Web MVC框架之一。 

如果有同學對它不熟悉,那么請參考它的入門blog:http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html

本文所講的部分內容跟SpringMVC的視圖機制有關,SpringMVC的視圖機制請參考樓主的另一篇博客:

http://www.cnblogs.com/fangjian0423/p/springMVC-view-viewResolver.html

RedirectView介紹

RedirectView這個視圖是跟重定向相關的,也是重定向問題的核心,我們來看看這個類的源碼。

RedirectView屬性:

幾個重要的構造方法:

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的重定向相關問題。

 

文中難免有錯誤,希望讀者能夠指明出來。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM