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}");
  • 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

    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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

}

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

转自http://blog.csdn.net/wilsonke/article/details/39177421

Spring mvc redirect跳转路径问题的更多相关文章

  1. spring mvc redirect 重定向 跳转并传递参数

    在项目中做form表单功能提交时,防止用户客户端后退或者刷新时重复提交问题,需要在服务端进行重定向跳转,具体跳转方式有以下几种方式: 公用代码: @RequestMapping(value=" ...

  2. Spring MVC 页面跳转时传递参数

    页面仍然使用 JSP,在跳转时如果想传递参数则需要用到类 RedirectAttributes. 首先看看如何打开一个普通页面: // 登录页面(每个页面都要独立的 Action 来支持其呈现) @R ...

  3. spring mvc redirect设置FlashAttribute

    在Controller中设置: @RequestMapping("/redir") public String redir(Model model, RedirectAttribu ...

  4. spring mvc controller接收请求值及controller之间跳转及传值

    spring接收请求参数: 1,使用HttpServletRequest获取 @RequestMapping("/login.do") public String login(Ht ...

  5. Spring MVC教程——检视阅读

    Spring MVC教程--检视阅读 参考 Spring MVC教程--一点--蓝本 Spring MVC教程--c语言中午网--3.0版本太老了 Spring MVC教程--易百--4.0版本不是通 ...

  6. Spring mvc get和post传值乱码问题

    1.url拼值 传单值 对象 list  map都是用json的格式传入后台 <%@ page language="java" contentType="text/ ...

  7. Spring + Spring MVC + Hibernate

    Spring + Spring MVC + Hibernate项目开发集成(注解) Posted on 2015-05-09 11:58 沐浴未来的我和你 阅读(307) 评论(0) 编辑 收藏 在自 ...

  8. Spring + Spring MVC + Hibernate项目开发集成(注解)

    在自己从事的项目中都是使用xml配置的方式来进行的,随着项目的越来越大,会发现配置文件会相当的庞大,这个不利于项目的进行和后期的维护.于是考虑使用注解的方式来进行项目的开发,前些日子就抽空学习了一下. ...

  9. Spring mvc框架 controller间跳转 ,重定向 ,传参

     一.需求背景     1. 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示.   @Req ...

随机推荐

  1. [Angular] Custom directive Form validator

    Create a directive to check no special characters allowed: import {Directive, forwardRef} from '@ang ...

  2. setting.system-全局属性的设定

    SystemProperties跟Settings.System 1 使用 SystemProperties.get如果属性名称以“ro.”开头,那么这个属性被视为只读属性.一旦设置,属性值不能改变. ...

  3. windows 批处理脚本(batch scripting)

    Guide to Windows Batch Scripting DOS 不需对变量事先声明.未声明或未初始化变量是一个空字符串("") 1. 变量赋值 set命令用于变量赋值.s ...

  4. 1.2 Use Cases中 Messaging官网剖析(博主推荐)

    不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 下面是一些关于Apache kafka 流行的使用场景.这些领域的概述,可查看博客文 ...

  5. JS实现联想自动补齐功能

    <!DOCTYPE HTML> <html> <head> <meta charset = "utf-8"> <title&g ...

  6. 16、cgminer学习之:popen函数和system函数详解(执行系统命令)

    1.popen函数我们先用man指令查一下popen函数: 函数说明: (1)popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c来执行参数command的指令. (2) ...

  7. Docker---(9)Docker中容器无法停止无法删除

    原文:Docker---(9)Docker中容器无法停止无法删除 版权声明:欢迎转载,请标明出处,如有问题,欢迎指正!谢谢!微信:w1186355422 https://blog.csdn.net/w ...

  8. 【习题 6-1 UVA-673】Parentheses Balance

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 括号匹配. 栈模拟就好. 多种括号也是一样可以做的. [代码] #include <bits/stdc++.h> usi ...

  9. Android通过startService播放背景音乐简单演示样例

    关于startService的基本使用概述及其生命周期可參见博客<Android中startService的使用及Service生命周期>. 本文通过播放背景音乐的简单演示样例,演示sta ...

  10. (笑话)切,我也是混血儿,我爸是A型血,我妈是B型血!

    1.中午,在家里看电视,电视里正在说起食品安全问题.侄儿突然感叹道:“现在的食品真不让人放心啊!”嘿,没想到侄儿小小年纪竟有这般认识,我正要抓住机会教育他不要乱吃零食.这时侄儿幽怨的瞪着我说:“我昨晚 ...