SpringMVC中的视图是View接口,视图的作用是渲染数据,将模型Model中的数据展示给用户
SpringMVC视图的种类很多,默认有转发视图(InternalResourceView)和重定向视图(RedirectView)
当工程引入jstl的依赖,转发视图会自动转换为JstlView
若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView
一. ThymeleafView
当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ViewController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/thymeleafView")
public String testThymeleafView(){
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/thymeleafView}">测试ThymeleafView</a>
</body>
</html>
二. InternalResourceView (转发视图)
当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部分作为最终路径通过转发的方式实现跳转。
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ViewController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/thymeleafView")
public String testThymeleafView(){
return "test";
}
@RequestMapping("/testForward")
public String testForward(){
return "forward:/thymeleafView"; //不可以用forward:+html来之间跳转到其他页面,可以写成"forward:/"来跳到index页面
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/testForward}">测试</a>
</body>
</html>
此时,先创建InternalResourceView视图,再创建ThymeleafView视图,依然可以跳转到test.html页面。此时到test.html页面地址栏是第一次请请求的地址:即 /testForward
三. RedirectView (重定向视图)
当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向的方式实现跳转
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ViewController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/thymeleafView")
public String testThymeleafView(){
return "test";
}
@RequestMapping("/testRedirect")
public String testRedirect(){
return "redirect:/thymeleafView"; //不可以用redirect:+html来之间跳转到其他页面,可以写成"redirect:/"来跳到index页面
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/testRedirect}">测试</a>
</body>
</html>
此时到test.html页面地址栏是第一次请请求的地址:即 /thymeleafView
四. view-controller (视图控制器)
当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view-controller标签进行表示。
当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签:<mvc:annotation-driven />
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:annotation-driven/> <!--开启MVC的注解驱动 -->
在SpringMVC的核心配置文件中添加上面的内容可以替代以下内容:
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ViewController {
@RequestMapping("/")
public String index(){
return "index";
}
}