SpringMVC学习笔记
六、SpringMVC的视图
SpringMVC中的视图是View接口,视图的作用渲染数据,将模型Model中的数据展示给用户;
SpringMVC视图的种类很多,默认有转发视图和重定向视图,当工程引入jstl的依赖,转发视图会自动转换为JstlView;若使用的视图技术为Thymeleaf,在SpringMVC的配置文件中配置了Thymeleaf的视图解析器,由此视图解析器解析之后所得到的是ThymeleafView。
6.1)ThymeleafView
当控制器方法中所设置的视图名称没有任何前缀时,此时的视图名称会被SpringMVC配置文件中所配置的视图解析器解析,视图名称拼接视图前缀和视图后缀所得到的最终路径,会通过转发的方式实现跳转,相关配置代码如下:springMVC.xml
<!--2)配置视图解析器-->
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"/>
<!-- 视图后缀 -->
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML5"/>
<property name="characterEncoding" value="UTF-8"/>
</bean>
</property>
</bean>
</property>
</bean>
示例代码如下:TestController.java
@Controller
public class TestController {
@RequestMapping("/test_view")
public String testView() {
return "test_view";
}
}
ViewController.java
/**
* Description: SpringMVC的视图
*/
@Controller
public class ViewController {
@RequestMapping("/testThymeleafView")
public String testThymeleafView() {
return "success";
}
}
test_view.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test_view</h1>
<a th:href="@{/testThymeleafView}">测试ThymeleafView</a><br>
</body>
</html>
【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/test_view】,点击超链接“测试ThymeleafView”,页面跳转至:http://localhost:8080/SpringMvcDemo3/testThymeleafView,页面如下:
代码走查流程如下:
1)doDispatch()方法
2)processDispatchResult()方法
3)render()方法【获取到要跳转页面的名称 “success”进行跳转】
6.2)转发视图
SpringMVC中默认的转发视图是InternalResourceView;
SpringMVC中创建转发视图的情况:
当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"forward:"去掉,剩余部分作为最终路径通过转发的方式实现跳转【会转发两次,第一次到转发到以"forward:"为前缀的请求,第二次转发到以第一次请求的ThymeleafView实现页面跳转】【不常用,直接以ThymeleafView转发实现页面跳转即可】;
示例代码如下:ViewController.java
@Controller
public class ViewController {
// 1)ThymeleafView
@RequestMapping("/testThymeleafView")
public String testThymeleafView() {
return "success";
}
// 2)转发视图
@RequestMapping("/testForward")
public String testForward() {
return "forward:/testThymeleafView";
}
}
修改 test_view.html,代码如下:
<a th:href="@{/testForward}">2)测试InternalResourceView</a><br>
【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/test_view】,点击超链接“测试InternalResourceView”,页面跳转至:http://localhost:8080/SpringMvcDemo3/testForward,页面如下:
代码走查流程如下:【主要流程与ThymeleafView一致,但会执行两次转发操作】
1)render()方法【第一次执行转发时,第一次到转发到以"forward:"为前缀的请求,创建InternalResourceView视图】
2)render()方法【第二次执行转发时,转发到以第一次请求的路径作为最终路径(ThymeleafView)通过转发的方式实现页面跳转】
6.3)重定向视图
6.3.1)转发和重定向的区别
转发:
浏览器发送一次请求,然后服务器内部发送一次请求;
浏览器地址栏仍然是第一次的请求地址;
可以获取请求域中的数据,因为都是同一个浏览器请求【都是:http://localhost:8080/SpringMvcDemo3/testForward】;
可以访问WEB-INF中的资源,具有安全性,只能服务器内部访问;
不能跨域访问,只能访问服务器内部资源
重定向:
浏览器发送两次请求,第一次访问Servlet,第二次访问重定向的地址;
浏览器地址栏是第二次重定向的请求地址;
不能获取请求域中的数据,因为是两个浏览器请求【第一次:http://localhost:8080/SpringMvcDemo3/testRedirect 第二次:http://localhost:8080/SpringMvcDemo3/testThymeleafView】;
不能访问WEB-INF中的资源,具有安全性,不能通过浏览器访问;
可以跨域访问,可以访问多个服务器资源,跨服务器调用
6.3.2)重定向的实现
SpringMVC中默认的重定向视图是RedirectView;
当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图,此时的视图名称不会被SpringMVC配置文件中所配置的视图解析器解析,而是会将前缀"redirect:"去掉,剩余部分作为最终路径通过重定向的方式实现跳转;
示例代码如下:ViewController.java
@Controller
public class ViewController {
// 1)ThymeleafView
@RequestMapping("/testThymeleafView")
public String testThymeleafView() {
return "success";
}
// 3)重定向视图
@RequestMapping("/testRedirect")
public String testRedirect() {
return "redirect:/testThymeleafView";
}
}
修改 test_view.html,代码如下:
<a th:href="@{/testRedirect}">3)测试RedirectView</a><br>
【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/test_view】,点击超链接“测试RedirectView”,页面跳转至:http://localhost:8080/SpringMvcDemo3/testThymeleafView,页面如下:
代码走查流程如下:【主要流程与ThymeleafView一致,但会执行两次转发操作】
1)render()方法【第一次执行转发时,第一次到转发到以"redirect:"为前缀的请求,创建RedirectView视图】
2)render()方法【第二次执行重定向时,转发到以第一次请求的路径作为最终路径(ThymeleafView)通过重定向的方式实现页面跳转】
重定向视图在解析时,会先将redirect:前缀去掉,然后会判断剩余部分是否以/开头,若是则会自动拼接上下文路径
6.4)视图控制器view-controller
当控制器方法中,仅仅用来实现页面跳转,即只需要设置视图名称时,可以将处理器方法使用view-controller标签进行表示,修改视图解析器springMVC.xml,代码如下:
<!--3)配置视图控制器-->
<!-- path:设置处理的请求地址 view-name:设置请求地址所对应的视图名称 -->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:view-controller path="/test_view" view-name="test_view"></mvc:view-controller>
将之前页面控制器 TestController中的跳转页面方法删除,代码如下:
@Controller
public class TestController {
}
【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/test_view】,点击超链接“测试RedirectView”,页面跳转至:http://localhost:8080/SpringMvcDemo3/testRedirect,页面如下:
当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签,修改视图解析器springMVC.xml,代码如下:
<!--3)配置视图控制器-->
<!-- path:设置处理的请求地址 view-name:设置请求地址所对应的视图名称 -->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:view-controller path="/test_view" view-name="test_view"></mvc:view-controller>
<!--开启mvc的注解驱动-->
<mvc:annotation-driven />
【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3/test_view】,点击超链接“测试RedirectView”,页面跳转至:http://localhost:8080/SpringMvcDemo3/testThymeleafView,页面如下:【可以正常访问】
6.5)原生JSP实现页面跳转
在源码中新建工程 SpringMvcDemo3Jsp,和SpingMVC中主要区别在视图解析器springMVC.xml中的视图解析器,代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描组件-->
<context:component-scan base-package="com.study.mvc.controller"></context:component-scan>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 视图前缀 -->
<property name="prefix" value="/WEB-INF/templates/"></property>
<!-- 视图后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
页面控制器 JspController.java
@Controller
public class JspController {
@RequestMapping("/success")
public String success() {
return "success";
}
}
首页 index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>首页</h1>
<%--动态获取上下文路径:${pageContext.request.contextPath}--%>
<a href="${pageContext.request.contextPath}/success">success.jsp</a>
</body>
</html>
跳转页 success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
成功
</body>
</html>
测试:【配置Tomacat后启动工程,浏览器访问:http://localhost:8080/SpringMvcDemo3Jsp/】,点击超链接“success.jsp”,页面跳转至:http://localhost:8080/SpringMvcDemo3Jsp/success,页面如下: