当前位置:首页 > 通信资讯 > 正文

spring boot 重定向(springboot return redirect)

目录
  • SpringMVC重定向视图RedirectView小分析
    • 前言
    • 实例讲解
      • Controller代码
      • 我们通过firebug看下路径:
    • 总结

    SpringMVC重定向视图RedirectView小分析

    前言

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

    本文所讲的部分内容跟SpringMVC的视图机制有关,SpringMVC的视图机制请参考楼主的另一篇博客:

    RedirectView这个视图是跟重定向相关的,也是重定向问题的核心,我们来看看这个类的源码。

    路径构造完毕之后使用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解析视图名的话,最调用父类之一的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的相对路径属性怪怪的,不使用相对路径,”/” 开头的直接就是服务器根路径, 不带 “/” 开头的,又是相对路径, 有点蛋疼。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

    原文链接:https://blog.csdn.net/qq_37992974/article/details/101455844

    如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

    为您推荐:

    发表评论

    ◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。