Spring MVC-视图
概括
Spring MVC定义了viewResolver和View接口,在浏览器中呈现模型中的数据。Spring MVC视图默认有转发视图(InternalResourceView)和重定向视图(RedirectView)
环境
IDE:idea 2020.3
构建工具:maven-3.8.1
服务器:tomcat9
Spring版本:5.3.1
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.1</version>
</dependency>
<!-- 日志 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
<!-- ServletAPI -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Spring5和Thymeleaf整合包 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.12.RELEASE</version>
</dependency>
</dependencies>
<?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">
<!-- springMVC.xml -->
<!-- 自动扫描包 -->
<context:component-scan base-package="com.fly.controller"/>
<!-- 配置Thymeleaf视图解析器 -->
<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>
</beans>
<!-- web.xml -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
ThymeleafView
当控制器方法的视图名称没有前缀时,视图名称会被视图解析器解析,通过视图解析器的前缀、后缀得到路径后以转发的形式跳转
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<!--index.html-->
<!--index.html-->
<a th:href="@{/testThymeleafView}">testThymeleafView</a><br/>
</body>
</html>
package com.fly.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author 26414
*/
@Controller
public class ViewController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testThymeleafView")
public String testThymeleafView() {
return "view";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>view</title>
</head>
<body>
<!--view.html-->
<h2>view</h2>
</body>
</html>
在testThymeleafView方法上打断点,以debug模式启动服务器
InternalResourceView
SpringMVC中默认的转发视图是InternalResourceView,当控制器方法中所设置的视图名称以"forward:"为前缀时,创建InternalResourceView视图。
/**
*ViewController
*/
@RequestMapping("/testInternalResourceView")
public String testInternalResourceView() {
return "forward:/testThymeleafView";
}
<!--index.html-->
<a th:href="@{/testInternalResourceView}">testInternalResourceView</a><br/>
在方法testInternalResourceView上打断点,以debug模式启动服务器
RedirectView
SpringMVC中默认的重定向视图是RedirectView,当控制器方法中所设置的视图名称以"redirect:"为前缀时,创建RedirectView视图。
/**
*ViewController
*/
@RequestMapping("/testRedirect")
public String testRedirect() {
return "redirect:/testThymeleafView";
}
<!--index.html-->
<a th:href="@{/testRedirect}">testRedirect</a><br/>
在方法testRedirect上打断点,以debug模式启动服务器
view-controller
如果一个控制器方法仅仅用来实现页面跳转,可以将处理器方法使用view-controller标签进行表示,当SpringMVC中设置任何一个view-controller时,其他控制器中的请求映射将全部失效,此时需要在SpringMVC的核心配置文件中设置开启mvc注解驱动的标签
<!-- springMVC.xml -->
<mvc:view-controller path="/" view-name="index"/>
<!-- 开启mvc注解驱动 -->
<mvc:annotation-driven />