目录

前言

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的重定向相关问题。

文中难免有错误,希望读者能够指明出来。

SpringMVC重定向视图RedirectView小分析的更多相关文章

  1. Django——基于类的视图源码分析 二

    源码分析 抽象类和常用视图(base.py) 这个文件包含视图的顶级抽象类(View),基于模板的工具类(TemplateResponseMixin),模板视图(TemplateView)和重定向视图 ...

  2. SpringMVC源码分析6:SpringMVC的视图解析原理

    title: SpringMVC源码分析6:SpringMVC的视图解析原理 date: 2018-06-07 11:03:19 tags: - SpringMVC categories: - 后端 ...

  3. SpringMVC核心——视图渲染(包含视图解析)问题

    一.本来想说的是返回值处理问题,但在 SpringMVC 中,返回值处理问题的核心就是视图渲染.所以这里标题叫视图渲染问题. 本来想在上一篇文章中对视图解析进行说明的,但是通过源码发现,它应该算到视图 ...

  4. SpringMvc:视图和视图解析器

    请求处理方法执行完成后,最终返回一个ModelAndView对象,对于返回String,View或ModelMap等类型的处理方法,SpringMvc也会在内部将它们装配成一个ModelAndView ...

  5. 什么时候会进行 SpringMVC重定向保存参数(FlashMap)?

    SpringMVC重定向保存参数(FlashMap):两种情况会保存参数:    1. 当前视图为RedirectView,也即是说当前请求为重定向请求.        org.springframe ...

  6. 【SpringMVC】视图

    SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户 SpringMVC视图的种类很多,默认有转发视图和重定向视图 当工程引入jstl的依赖,转发视图会自动 ...

  7. 项目总结SpringMVC+hibernate框架 web.xml 分析(2)

    紧接 项目总结SpringMVC+hibernate框架 原理(MVC) applicationContext.xml 文件(3) 这一步讲解项目模块化的配置,项目中每个模块配置一个文件,命名规则为 ...

  8. SpringMVC由浅入深day01_6源码分析(了解)

    6 源码分析(了解) 通过前端控制器源码分析springmvc的执行过程. 入口 第一步:前端控制器接收请求 调用doDiapatch 第二步:前端控制器调用处理器映射器查找 Handler 第三步: ...

  9. springmvc重定向请求。

    SpringMVC重定向传参数的实现(来自网友) 验证了我说的,从model层中拿来的数据,不管什么类型,都是通过隐含模型,中转,放入request中的.除非你特意把这些数据放到session域中. ...

随机推荐

  1. Bootstrap 类解析

    Bootstrap 类解析 元素 Bootstrap 类 定义 <div> container 内容容器 <table> table 表格 <table> tabl ...

  2. JS的三种弹框

    <script type="text/javascript"> /*第一种*/ function ale(){ alert("这是第一种");} / ...

  3. iOS开发-微博客户端-基本界面搭建(01)

    1>创建程序载入界面 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDict ...

  4. iTOP-4412开发板---Linux系统学习下载步骤

    本文转自迅为论坛:http://www.topeetboard.com 1.cd /home/topeet/Linux-simple/console  下建立.c文件 2. 编译命令,就在此目录下 # ...

  5. UI设计实战篇——利用Bootstrap框架制作查询页面的界面

    Bootstrap框架是一个前端UI设计的框架,它提供了统一的UI界面,简化了设计界面UI的过程(缺点是定制了界面,调整的余地不是太大).尤其是现在的响应时布局(我的理解是页面根据不同的分辨率,采用不 ...

  6. SharePoint Error:a system restart from a previous installation or update is pending

    run:regedit 打开注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager 删除PendingFileR ...

  7. CSS select样式优化 含jquery代码

    CSS 下拉选择菜单基本的CSS样式不怎么好看,通过一些简单的样式优化,可以得到如下图这样的: html结构如下: <div class="sel_wrap"> < ...

  8. Unity使用 UnityVS+VS2013 调试脚本

    好消息:UnityVS免费啦 好消息:微软收购了UnityVS公司,UnityVS免费啦 下载地址:https://visualstudiogallery.msdn.microsoft.com/sit ...

  9. GetComponent

    GetComponent 的几种写法: 1.AutoRotation cmp1=(AutoRotation) GetComponent(typeof(AutoRotation)); 2.AutoRot ...

  10. Hibernate出现javax.naming.NoInitialContextException 错误的解决办法

    异常信息: 08:02:56,329 WARN SessionFactoryObjectFactory:123 - Could not unbind factory from JNDI javax.n ...