SpringMVC6-SpringMVC的视图

目录

ThymeleafView

转发视图

重定向视图

视图控制器view-controller


SpringMVC中的视图是View接口,视图的作用:渲染数据,将模型Model中的数据展示给用户

SpringMVC视图的种类很多,默认有转发视图InternalResourceView 和重定向视图RedirectView

当工程引入jstl的依赖,转发视图会自动转换为JstlView

若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView

ThymeleafView

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

java 复制代码
package com.qcby.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {

    @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
        return "success";
    }
}
java 复制代码
package com.qcby.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

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

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

test_view.html:

html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/testThymeleafView}">测试ThymeleafView</a>
</body>
</html>

运行:

转发视图

SpringMVC中默认的转发视图是InternalResourceView

SpringMVC中创建转发视图的情况:

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

例如"forward:/","forward:/employee"

java 复制代码
package com.qcby.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {

    @RequestMapping("/testThymeleafView")
    public String testThymeleafView(){
        return "success";
    }
    
    @RequestMapping("/testForward")
    public String testForward(){
        return "forward:/testThymeleafView";
    }
}
html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/testForward}">测试InternalResourceView</a>
</body>
</html>

运行:

页面路径为/testForward

重定向视图

SpringMVC中默认的重定向视图是RedirectView

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

例如"redirect:/","redirect:/employee"

重定向视图在解析时,会先将redirect:前缀去掉,然后会判断剩余部分是否以/开头,若是则会自 动拼接上下文路径

java 复制代码
package com.qcby.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ViewController {

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

    @RequestMapping("/testRedirect")
    public String testRedirect(){
        return "redirect:/testThymeleafView";
    }
}
html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<a th:href="@{/testRedirect}">测试RedirectView</a>
</body>
</html>

运行:

页面路径为/testThymeleafView

视图控制器view-controller

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

springMVC.xml:

XML 复制代码
<!--
 path:设置处理的请求地址
 view-name:设置请求地址所对应的视图名称
-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>

代替:

java 复制代码
package com.qcby.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {

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

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

XML 复制代码
<!--开启MVC的注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
相关推荐
爱喝一杯白开水1 天前
SpringMVC从入门到上手-全面讲解SpringMVC的使用.
java·spring·springmvc
V功夫兔5 天前
Spring_MVC 快速入门指南
java·笔记·spring·springmvc
岁岁岁平安9 天前
SpringMVC入门学习总结(2025.04.16)
java·spring·java-ee·mvc·springmvc
在努力的韩小豪20 天前
SpringMVC和SpringBoot是否线程安全?
spring boot·后端·springmvc·线程安全·bean的作用域
依旧很淡定1 个月前
05-SpringBoot3入门-整合SpringMVC(配置静态资源、拦截器)
springmvc·拦截器·静态资源
就改了1 个月前
SpringMVC 跨域问题两种常用解决方案
java·springmvc
一弓虽1 个月前
springmvc 框架学习
java·学习·spring·ssm·springmvc
字节源流1 个月前
【SpringMVC】常用注解:@MatrixVariable
springmvc
想要打 Acm 的小周同学呀1 个月前
SpringMVC执行的流程
springmvc·springmvc执行的流程
工一木子1 个月前
【SpringMVC】SpringMVC的启动过程与原理分析:从源码到实战
mvc·springmvc·原理分析