4

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解

 1 year ago
source link: https://blog.51cto.com/u_15740728/5918839
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户
SpringMVC视图的种类很多,默认有转发视图重定向视图
当工程引入jstl的依赖,转发视图会自动转换为JstlView
若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视
图解析器解析之后所得到的是ThymeleafView

1、ThymeleafView

当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置
的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转

1.1 核心代码


    /**
     * 测试testThymeleafView
     * @return
     */
    @RequestMapping("/testThymeleafView")
    public String testView(){
        return "success";
    }

1.2 测试结果

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_MVC

1.3 通过源码查看

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_MVC_02

2、转发视图

SpringMVC中默认的转发视图是InternalResourceView
SpringMVC中创建转发视图的情况:
当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视
图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:“去掉,剩余部分作为最终路径通过转发的方式实现跳转
例如"forward:/”,“forward:/employee”

2.1 核心代码

    @RequestMapping("/testThymeleafView")
    public String testView(){
        return "success";
    }

    /**
     * 测试forward
     * @return
     */
    @RequestMapping("/testForward")
    public String testForward(){
        return "forward:/testThymeleafView";
    }

2.2 测试结果

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_配置文件_03

2.3 源码查看

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_配置文件_04

3、重定向视图

SpringMVC中默认的重定向视图是RedirectView
当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不
会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:“去掉,剩余部分作为最
终路径通过重定向的方式实现跳转
例如"redirect:/”,“redirect:/employee”

3.1 核心代码

    @RequestMapping("/testThymeleafView")
    public String testView(){
        return "success";
    }


    /**
     * 测试redirect 重定向
     * @return
     */
    @RequestMapping("/testRedirect")
    public String testRedirect(){
        return "redirect:/testThymeleafView";
    }

3.2 测试结果

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_配置文件_05

3.3 源码查看

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_配置文件_06

4、视图控制器view-controller

当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用viewcontroller标签进行表示

<!--
path:设置处理的请求地址
view-name:设置请求地址所对应的视图名称
-->
<mvc:view-controller path="/testView" view-name="success"></mvc:view-controller>

注:
当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需
要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:
<mvc:annotation-driven />

4.1 核心代码

  <mvc:view-controller path="/" view-name="index"></mvc:view-controller>

代替

    @RequestMapping("/")
    public String toIndex() {
        return "index";
    }

需要添加注解驱动、否则跳转连接无效

    <!--开启mvc的注解驱动-->
    <mvc:annotation-driven/>
SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_插入图片_07

4.2 开启后、正常跳转

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_插入图片_08

4.3 如果不开启、跳转失败

SpringMVC的视图。ThymeleafView、转发视图、重定向视图、视图控制器的使用详解_配置文件_09

学无止境。。。。。。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK