SpringMVC 也叫 Spring web mvc,是 Spring 框架的一部分,在 Spring 3.0 后发布,基于 MVC 架构,功能分工明确、解耦合。
其核心思想是**以 Servlet 为核心,利用 IoC 容器统一管理界面层(Controller)对象**,让开发者以更简洁、更解耦的方式接收请求、绑定参数、转发业务并返回响应结果。
SpringMVC 也是一个容器,使用 IoC 核心技术,管理界面层中的控制器对象。SpringMVC 的底层就是 servlet,以 servlet 为核心,接收请求、处理请求,显示处理结果给用户。在此之前这个功能是由 Servlet 来实现的,现在使用 SpringMVC 来代替 Servlet 行使控制器的角色和功能。
1. 概述
学习 SpringMVC 我们先来回顾下现在 web 程序是如何做的,咱们现在 web 程序大都基于三层架构来实现。
1.1 三层架构与 MVC
三层架构
- 浏览器发送一个请求给后端服务器,后端服务器现在是使用 Servlet 来接收请求和数据。
- 如果所有的处理都交给 Servlet 来处理的话,所有的东西都耦合在一起,对后期的维护和扩展极为不利。
- 将后端服务器 Servlet 拆分成三层,分别是
web、service和dao。- web 层主要由 servlet 来处理,负责页面请求和数据的收集以及响应结果给前端。
- service 层主要负责业务逻辑的处理。
- dao 层主要负责数据的增删改查操作。
- servlet 处理请求和数据的时候,存在的问题是一个 servlet 只能处理一个请求。
- 针对 web 层进行了优化,采用了 MVC 设计模式,将其设计为
controller、view和Model。- controller 负责请求和数据的接收,接收后将其转发给 service 进行业务处理。
- service 根据需要会调用 dao 对数据进行增删改查。
- dao 把数据处理完后将结果交给 service,service 再交给 controller。
- controller 根据需求组装成 Model 和 View,Model 和 View 组合起来生成页面转发给前端浏览器。
- 这样做的好处就是 controller 可以处理多个请求,并对请求进行分发,执行不同的业务操作。
随着互联网的发展,上面的模式因为是同步调用,性能慢慢的跟不是需求,所以异步调用慢慢的走到了前台,是现在比较流行的一种处理方式。
- 因为是异步调用,所以后端不需要返回 view 视图,将其去除。
- 前端如果通过异步调用的方式进行交互,后台就需要将返回的数据转换成 json 格式进行返回。
- SpringMVC 主要负责的就是:
- controller 如何接收请求和数据。
- 如何将请求和数据转发给业务层。
- 如何将响应数据转换成 json 发回到前端。
1.2 SpringMVC 定义与优点
介绍了这么多,对 SpringMVC 进行一个定义。
- SpringMVC 是一种基于 Java 实现 MVC 模型的轻量级 Web 框架。
- 优点:
- 使用简单、开发便捷(相比于 Servlet)。
- 灵活性强。
这里所说的优点,就需要我们在使用的过程中慢慢体会。
2. 入门案例
因为 SpringMVC 是一个 Web 框架,将来是要替换 Servlet,所以先来回顾下以前 Servlet 是如何进行开发的?
- 创建 web 工程(Maven 结构)。
- 设置 tomcat 服务器,加载 web 工程(tomcat 插件)。
- 导入坐标(Servlet)。
- 定义处理请求的功能类(UserServlet)。
- 设置请求映射(配置映射关系)。
SpringMVC 的制作过程和上述流程几乎是一致的,具体的实现流程是什么?
- 创建 web 工程(Maven 结构)。
- 设置 tomcat 服务器,加载 web 工程(tomcat 插件)。
- 导入坐标(SpringMVC+Servlet)。
- 定义处理请求的功能类(UserController)。
- 设置请求映射(配置映射关系)。
- 将 SpringMVC 设定加载到 Tomcat 容器中。
2.1 需求分析
(本节为入门案例的需求背景,已在上方流程中说明。)
2.2 案例制作
步骤1:创建 Maven 项目
打开 IDEA,创建一个新的 web 项目。
步骤2:补全目录结构
因为使用骨架创建的项目结构不完整,需要手动补全。
步骤3:导入 jar 包
将 pom.xml 中多余的内容删除掉,再添加 SpringMVC 需要的依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_01_quickstart</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
说明:servlet 的坐标为什么需要添加 <scope>provided</scope>?
- scope 是 maven 中 jar 包依赖作用范围的描述。
- 如果不设置默认是
compile,在编译、运行、测试时均有效。 - 如果运行有效的话就会和 tomcat 中的 servlet-api 包发生冲突,导致启动报错。
- provided 代表的是该包只在编译和测试的时候用,运行的时候无效直接使用 tomcat 中的,就避免冲突。
步骤4:创建配置类
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {
}
步骤5:创建 Controller 类
@Controller
public class UserController {
@RequestMapping("/save")
public void save(){
System.out.println("user save ...");
}
}
步骤6:使用配置类替换 web.xml
将 web.xml 删除,换成 ServletContainersInitConfig。
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
//加载springmvc配置类
protected WebApplicationContext createServletApplicationContext() {
//初始化WebApplicationContext对象
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
//加载指定配置类
ctx.register(SpringMvcConfig.class);
return ctx;
}
//设置由springmvc控制器处理的请求映射路径
protected String[] getServletMappings() {
return new String[]{"/"};
}
//加载spring配置类
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
步骤7:配置 Tomcat 环境
步骤8:启动运行项目
步骤9:浏览器访问
浏览器输入 http://localhost/save 进行访问,会报如下错误:
页面报错的原因是后台没有指定返回的页面,目前只需要关注控制台看 user save ... 有没有被执行即可。
步骤10:修改 Controller 返回值解决上述问题
前面我们说过现在主要的是前端发送异步请求,后台响应 json 数据,所以接下来我们把 Controller 类的 save 方法进行修改。
@Controller
public class UserController {
@RequestMapping("/save")
public String save(){
System.out.println("user save ...");
return "{'info':'springmvc'}";
}
}
再次重启 tomcat 服务器,然后重新通过浏览器测试访问,会发现还是会报错,这次的错是 404。
出错的原因是,如果方法直接返回字符串,springmvc 会把字符串当成页面的名称在项目中进行查找返回,因为不存在对应返回值名称的页面,所以会报 404 错误,找不到资源。
而我们其实是想要直接返回的是 json 数据,具体如何修改呢?
步骤11:设置返回数据为 json
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'info':'springmvc'}";
}
}
再次重启 tomcat 服务器,然后重新通过浏览器测试访问,就能看到返回的结果数据。
至此 SpringMVC 的入门案例就已经完成。
2.3 入门案例总结
- 一次性工作
- 创建工程,设置服务器,加载工程
- 导入坐标
- 创建 web 容器启动类,加载 SpringMVC 配置,并设置 SpringMVC 请求拦截路径
- SpringMVC 核心配置类(设置配置类,扫描 controller 包,加载 Controller 控制器 bean)
- 多次工作
- 定义处理请求的控制器类
- 定义处理请求的控制器方法,并配置映射路径(@RequestMapping)与返回 json 数据(@ResponseBody)
- SpringMVC 是基于 Spring 的,在 pom.xml 只导入了
spring-webmvcjar 包的原因是它会自动依赖 spring 相关坐标。 - AbstractDispatcherServletInitializer 类是 SpringMVC 提供的快速初始化 Web3.0 容器的抽象类。
- AbstractDispatcherServletInitializer 提供了三个接口方法供用户实现:
- createServletApplicationContext 方法,创建 Servlet 容器时,加载 SpringMVC 对应的 bean 并放入 WebApplicationContext 对象范围中,而 WebApplicationContext 的作用范围为 ServletContext 范围,即整个 web 容器范围。
- getServletMappings 方法,设定 SpringMVC 对应的请求映射路径,即 SpringMVC 拦截哪些请求。
- createRootApplicationContext 方法,如果创建 Servlet 容器时需要加载非 SpringMVC 对应的 bean,使用当前方法进行,使用方式和 createServletApplicationContext 相同。
- createServletApplicationContext 用来加载 SpringMVC 环境。
- createRootApplicationContext 用来加载 Spring 环境。
2.4 核心注解知识点
知识点1:@Controller
| 名称 | @Controller |
|---|---|
| 类型 | 类注解 |
| 位置 | SpringMVC 控制器类定义上方 |
| 作用 | 设定 SpringMVC 的核心控制器 bean |
知识点2:@RequestMapping
| 名称 | @RequestMapping |
|---|---|
| 类型 | 类注解或方法注解 |
| 位置 | SpringMVC 控制器类或方法定义上方 |
| 作用 | 设置当前控制器方法请求访问路径 |
| 相关属性 | value(默认),请求访问路径 |
知识点3:@ResponseBody
| 名称 | @ResponseBody |
|---|---|
| 类型 | 类注解或方法注解 |
| 位置 | SpringMVC 控制器类或方法定义上方 |
| 作用 | 设置当前控制器方法响应内容为当前返回值,无需解析 |
2.5 工作流程解析
为了更好的使用 SpringMVC,我们将 SpringMVC 的使用过程总共分两个阶段来分析,分别是 启动服务器初始化过程 和 单次请求过程。
2.5.1 启动服务器初始化过程
- 服务器启动,执行 ServletContainersInitConfig 类,初始化 web 容器。
- 功能类似于以前的 web.xml。
- 执行 createServletApplicationContext 方法,创建了 WebApplicationContext 对象。
- 该方法加载 SpringMVC 的配置类 SpringMvcConfig 来初始化 SpringMVC 的容器。
- 加载 SpringMvcConfig 配置类。
- 执行 @ComponentScan 加载对应的 bean。
- 扫描指定包及其子包下所有类上的注解,如 Controller 类上的 @Controller 注解。
- 加载 UserController,每个 @RequestMapping 的名称对应一个具体的方法。
- 此时就建立了
/save和 save 方法的对应关系。
- 此时就建立了
- 执行 getServletMappings 方法,设定 SpringMVC 拦截请求的路径规则。
/代表所拦截请求的路径规则,只有被拦截后才能交给 SpringMVC 来处理请求。
2.5.2 单次请求过程
- 发送请求
http://localhost/save。 - web 容器发现该请求满足 SpringMVC 拦截规则,将请求交给 SpringMVC 处理。
- 解析请求路径 /save。
- 由 /save 匹配执行对应的方法 save()。
- 上面的第五步已经将请求路径和方法建立了对应关系,通过 /save 就能找到对应的 save 方法。
- 执行 save()。
- 检测到有 @ResponseBody 直接将 save() 方法的返回值作为响应体返回给请求方。
2.6 bean 加载控制
2.6.1 问题分析
入门案例的内容已经做完了,在入门案例中我们创建过一个 SpringMvcConfig 的配置类,再回想前面咱们学习 Spring 的时候也创建过一个配置类 SpringConfig。这两个配置类都需要加载资源,那么它们分别都需要加载哪些内容?
我们先来看下目前我们的项目目录结构:
- config 目录存入的是配置类,写过的配置类有:
- ServletContainersInitConfig
- SpringConfig
- SpringMvcConfig
- JdbcConfig
- MybatisConfig
- controller 目录存放的是 SpringMVC 的 controller 类。
- service 目录存放的是 service 接口和实现类。
- dao 目录存放的是 dao/Mapper 接口。
controller、service 和 dao 这些类都需要被容器管理成 bean 对象,那么到底是该让 SpringMVC 加载还是让 Spring 加载呢?
- SpringMVC 加载其相关 bean(表现层 bean),也就是 controller 包下的类。
- Spring 控制的 bean:
- 业务 bean(Service)。
- 功能 bean(DataSource、SqlSessionFactoryBean、MapperScannerConfigurer 等)。
分析清楚谁该管哪些 bean 以后,接下来要解决的问题是如何让 Spring 和 SpringMVC 分开加载各自的内容。
在 SpringMVC 的配置类 SpringMvcConfig 中使用注解 @ComponentScan,我们只需要将其扫描范围设置到 controller 即可。在 Spring 的配置类 SpringConfig 中使用注解 @ComponentScan,当时扫描的范围中其实是已经包含了 controller。
从包结构来看的话,Spring 已经把 SpringMVC 的 controller 类也给扫描到,所以针对这个问题该如何解决,就是咱们接下来要学习的内容。
概括的描述下咱们现在的问题就是:因为功能不同,如何避免 Spring 错误加载到 SpringMVC 的 bean?
2.6.2 思路分析
针对上面的问题,解决方案也比较简单,就是:
- 加载 Spring 控制的 bean 的时候排除掉 SpringMVC 控制的 bean。
具体该如何排除:
- 方式一:Spring 加载的 bean 设定扫描范围为精准范围,例如 service 包、dao 包等。
- 方式二:Spring 加载的 bean 设定扫描范围为 com.itheima,排除掉 controller 包中的 bean。
- 方式三:不区分 Spring 与 SpringMVC 的环境,加载到同一个环境中(了解即可)。
2.6.3 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 Spring 依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_02_bean_load</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建对应的配置类。
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {
}
@Configuration
@ComponentScan("com.itheima")
public class SpringConfig {
}
- 编写 Controller、Service、Dao、Domain 类。
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'info':'springmvc'}";
}
}
public interface UserService {
public void save(User user);
}
@Service
public class UserServiceImpl implements UserService {
public void save(User user) {
System.out.println("user service ...");
}
}
public interface UserDao {
@Insert("insert into tbl_user(name,age)values(#{name},#{age})")
public void save(User user);
}
public class User {
private Integer id;
private String name;
private Integer age;
//setter..getter..toString略
}
最终创建好的项目结构如下:
2.6.4 设置 bean 加载控制
方式一:修改 Spring 配置类,设定扫描范围为精准范围。
@Configuration
@ComponentScan({"com.itheima.service","comitheima.dao"})
public class SpringConfig {
}
说明:
上述只是通过例子说明可以精确指定让 Spring 扫描对应的包结构,真正在做开发的时候,因为 Dao 最终是交给 MapperScannerConfigurer 对象来进行扫描处理的,我们只需要将其扫描到 service 包即可。
方式二:修改 Spring 配置类,设定扫描范围为 com.itheima,排除掉 controller 包中的 bean。
@Configuration
@ComponentScan(value="com.itheima",
excludeFilters=@ComponentScan.Filter(
type = FilterType.ANNOTATION,
classes = Controller.class
)
)
public class SpringConfig {
}
- excludeFilters 属性:设置扫描加载 bean 时,排除的过滤规则。
- type 属性:设置排除规则,当前使用按照 bean 定义时的注解类型进行排除。
| type 取值 | 说明 |
|---|---|
| ANNOTATION | 按照注解排除 |
| ASSIGNABLE_TYPE | 按照指定的类型过滤 |
| ASPECTJ | 按照 Aspectj 表达式排除,基本上不会用 |
| REGEX | 按照正则表达式排除 |
| CUSTOM | 按照自定义规则排除 |
大家只需要知道第一种 ANNOTATION 即可。
- classes 属性:设置排除的具体注解类,当前设置排除 @Controller 定义的 bean。
如何测试 controller 类已经被排除掉了?
public class App{
public static void main (String[] args){
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
System.out.println(ctx.getBean(UserController.class));
}
}
如果被排除了,该方法执行就会报 bean 未被定义的错误。
测试的时候,需要把 SpringMvcConfig 配置类上的 @ComponentScan 注解注释掉,否则不会报错。
出现问题的原因是:
- Spring 配置类扫描的包是
com.itheima。 - SpringMVC 的配置类,
SpringMvcConfig上有一个 @Configuration 注解,也会被 Spring 扫描到。 - SpringMvcConfig 上又有一个 @ComponentScan,把 controller 类又给扫描进来了。
- 所以如果不把 @ComponentScan 注释掉,Spring 配置类将 Controller 排除,但是因为扫描到 SpringMVC 的配置类,又将其加载回来,演示的效果就出不来。
- 解决方案,也简单,把 SpringMVC 的配置类移出 Spring 配置类的扫描范围即可。
最后一个问题,有了 Spring 的配置类,要想在 tomcat 服务器启动将其加载,我们需要修改 ServletContainersInitConfig。
public class ServletContainersInitConfig extends AbstractDispatcherServletInitializer {
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
return ctx;
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected WebApplicationContext createRootApplicationContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringConfig.class);
return ctx;
}
}
对于上述的配置方式,Spring 还提供了一种更简单的配置方式,可以不用再去创建 AnnotationConfigWebApplicationContext 对象,不用手动 register 对应的配置类,如何实现?
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
知识点:@ComponentScan
| 名称 | @ComponentScan |
|---|---|
| 类型 | 类注解 |
| 位置 | 类定义上方 |
| 作用 | 设置 spring 配置类扫描路径,用于加载使用注解格式定义的 bean |
| 相关属性 | excludeFilters:排除扫描路径中加载的 bean,需要指定类别(type)和具体项(classes) includeFilters:加载指定的 bean,需要指定类别(type)和具体项(classes) |
3. PostMan 工具的使用
3.1 PostMan 简介
代码编写完后,我们要想测试,只需要打开浏览器直接输入地址发送请求即可。发送的是 GET 请求可以直接使用浏览器,但是如果要发送的是 POST 请求呢?
如果要求发送的是 post 请求,我们就得准备页面在页面上准备 form 表单,测试起来比较麻烦。所以我们就需要借助一些第三方工具,如 PostMan。
- PostMan 是一款功能强大的网页调试与发送网页 HTTP 请求的 Chrome 插件。
- 作用:常用于进行接口测试。
- 特征:
- 简单
- 实用
- 美观
- 大方
3.2 PostMan 安装
双击 资料\Postman-win64-8.3.1-Setup.exe 即可自动安装。
安装完成后,如果需要注册,可以按照提示进行注册,如果底部有跳过测试的链接也可以点击跳过注册。
第一次请求需要创建一个新的目录,后面就不需要创建新目录,直接保存到已经创建好的目录即可。
4. 请求与响应
前面我们已经完成了入门案例相关的知识学习,接来了我们就需要针对 SpringMVC 相关的知识点进行系统的学习,之前我们提到过,SpringMVC 是 web 层的框架,主要的作用是接收请求、接收数据、响应结果,所以这一章节是学习 SpringMVC 的重点内容,我们主要会讲解四部分内容:
- 请求映射路径
- 请求参数
- 日期类型参数传递
- 响应 json 数据
4.1 设置请求映射路径
4.1.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 Spring 依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_03_request_mapping</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建对应的配置类。
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
}
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {
}
- 编写 BookController 和 UserController。
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'module':'user save'}";
}
@RequestMapping("/delete")
@ResponseBody
public String save(){
System.out.println("user delete ...");
return "{'module':'user delete'}";
}
}
@Controller
public class BookController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("book save ...");
return "{'module':'book save'}";
}
}
把环境准备好后,启动 Tomcat 服务器,后台会报错:
从错误信息可以看出:
- UserController 有一个 save 方法,访问路径为
http://localhost/save。 - BookController 也有一个 save 方法,访问路径为
http://localhost/save。 - 当访问
http://localhost/saved的时候,到底是访问 UserController 还是 BookController?
4.1.2 问题分析
团队多人开发,每人设置不同的请求路径,冲突问题该如何解决?
解决思路:为不同模块设置模块名作为请求路径前置。
- 对于 Book 模块的 save,将其访问路径设置
http://localhost/book/save。 - 对于 User 模块的 save,将其访问路径设置
http://localhost/user/save。
这样在同一个模块中出现命名冲突的情况就比较少了。
4.1.3 设置映射路径
步骤1:修改 Controller
@Controller
public class UserController {
@RequestMapping("/user/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'module':'user save'}";
}
@RequestMapping("/user/delete")
@ResponseBody
public String save(){
System.out.println("user delete ...");
return "{'module':'user delete'}";
}
}
@Controller
public class BookController {
@RequestMapping("/book/save")
@ResponseBody
public String save(){
System.out.println("book save ...");
return "{'module':'book save'}";
}
}
问题是解决了,但是每个方法前面都需要进行修改,写起来比较麻烦而且还有很多重复代码,如果 /user 后期发生变化,所有的方法都需要改,耦合度太高。
步骤2:优化路径配置
优化方案:
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("user save ...");
return "{'module':'user save'}";
}
@RequestMapping("/delete")
@ResponseBody
public String save(){
System.out.println("user delete ...");
return "{'module':'user delete'}";
}
}
@Controller
@RequestMapping("/book")
public class BookController {
@RequestMapping("/save")
@ResponseBody
public String save(){
System.out.println("book save ...");
return "{'module':'book save'}";
}
}
- 当类上和方法上都添加了
@RequestMapping注解,前端发送请求的时候,要和两个注解的 value 值相加匹配才能访问到。 - @RequestMapping 注解 value 属性前面加不加
/都可以。
扩展小知识:
对于 PostMan 如果觉得字小不好看,可以使用 ctrl+= 调大,ctrl+- 调小。
4.2 请求参数
请求路径设置好后,只要确保页面发送请求地址和后台 Controller 类中配置的路径一致,就可以接收到前端的请求,接收到请求后,如何接收页面传递的参数?
关于请求参数的传递与接收是和请求方式有关系的,目前比较常见的两种请求方式为:
- GET
- POST
针对于不同的请求前端如何发送,后端如何接收?
4.2.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 Spring 依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_03_request_mapping</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建对应的配置类。
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
}
@Configuration
@ComponentScan("com.itheima.controller")
public class SpringMvcConfig {
}
- 编写 UserController。
@Controller
public class UserController {
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(){
return "{'module':'commonParam'}";
}
}
- 编写模型类,User 和 Address。
public class Address {
private String province;
private String city;
//setter...getter...略
}
public class User {
private String name;
private int age;
//setter...getter...略
}
最终创建好的项目结构如下:
4.2.2 参数传递
GET 发送单个参数
发送请求与参数:
http://localhost/commonParam?name=itcast
接收参数:
@Controller
public class UserController {
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(String name){
System.out.println("普通参数传递 name ==> "+name);
return "{'module':'commonParam'}";
}
}
GET 发送多个参数
发送请求与参数:
http://localhost/commonParam?name=itcast&age=15
接收参数:
@Controller
public class UserController {
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(String name,int age){
System.out.println("普通参数传递 name ==> "+name);
System.out.println("普通参数传递 age ==> "+age);
return "{'module':'commonParam'}";
}
}
GET 请求中文乱码
如果我们传递的参数中有中文,你会发现接收到的参数会出现中文乱码问题。
发送请求:http://localhost/commonParam?name=张三&age=18
控制台:
出现乱码的原因相信大家都清楚,Tomcat8.5 以后的版本已经处理了中文乱码的问题,但是 IDEA 中的 Tomcat 插件目前只到 Tomcat7,所以需要修改 pom.xml 来解决 GET 请求中文乱码问题。
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port><!--tomcat端口号-->
<path>/</path> <!--虚拟目录-->
<uriEncoding>UTF-8</uriEncoding><!--访问路径编解码字符集-->
</configuration>
</plugin>
</plugins>
</build>
POST 发送参数
发送请求与参数:
和 GET 一致,不用做任何修改。
@Controller
public class UserController {
@RequestMapping("/commonParam")
@ResponseBody
public String commonParam(String name,int age){
System.out.println("普通参数传递 name ==> "+name);
System.out.println("普通参数传递 age ==> "+age);
return "{'module':'commonParam'}";
}
}
POST 请求中文乱码
发送请求与参数:
接收参数:
控制台打印,会发现有中文乱码问题。
解决方案:配置过滤器。
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
CharacterEncodingFilter 是在 spring-web 包中,所以用之前需要导入对应的 jar 包。
4.3 五种类型参数传递
前面我们已经能够使用 GET 或 POST 来发送请求和数据,所携带的数据都是比较简单的数据,接下来在这个基础上,我们来研究一些比较复杂的参数传递,常见的参数种类有:
- 普通参数
- POJO 类型参数
- 嵌套 POJO 类型参数
- 数组类型参数
- 集合类型参数
这些参数如何发送,后台该如何接收?我们一个个来学习。
4.3.1 普通参数
- 普通参数:url 地址传参,地址参数名与形参变量名相同,定义形参即可接收参数。
如果形参与地址参数名不一致该如何解决?
发送请求与参数:
http://localhost/commonParamDifferentName?name=张三&age=18
后台接收参数:
@RequestMapping("/commonParamDifferentName")
@ResponseBody
public String commonParamDifferentName(String userName , int age){
System.out.println("普通参数传递 userName ==> "+userName);
System.out.println("普通参数传递 age ==> "+age);
return "{'module':'common param different name'}";
}
因为前端给的是 name,后台接收使用的是 userName,两个名称对不上,导致接收数据失败:
解决方案:使用 @RequestParam 注解。
@RequestMapping("/commonParamDifferentName")
@ResponseBody
public String commonParamDifferentName(@RequestPaam("name") String userName , int age){
System.out.println("普通参数传递 userName ==> "+userName);
System.out.println("普通参数传递 age ==> "+age);
return "{'module':'common param different name'}";
}
写上 @RequestParam 注解框架就不需要自己去解析注入,能提升框架处理性能。
4.3.2 POJO 数据类型
简单数据类型一般处理的是参数个数比较少的请求,如果参数比较多,那么后台接收参数的时候就比较复杂,这个时候我们可以考虑使用 POJO 数据类型。
- POJO 参数:请求参数名与形参对象属性名相同,定义 POJO 类型形参即可接收参数。
此时需要使用前面准备好的 POJO 类,先来看下 User。
public class User {
private String name;
private int age;
//setter...getter...略
}
发送请求和参数:
后台接收参数:
//POJO参数:请求参数与形参对象中的属性对应即可完成参数传递
@RequestMapping("/pojoParam")
@ResponseBody
public String pojoParam(User user){
System.out.println("pojo参数传递 user ==> "+user);
return "{'module':'pojo param'}";
}
- POJO 参数接收,前端 GET 和 POST 发送请求数据的方式不变。
- 请求参数 key 的名称要和 POJO 中属性的名称一致,否则无法封装。
4.3.3 嵌套 POJO 类型参数
如果 POJO 对象中嵌套了其他的 POJO 类,如:
public class Address {
private String province;
private String city;
//setter...getter...略
}
public class User {
private String name;
private int age;
private Address address;
//setter...getter...略
}
- 嵌套 POJO 参数:请求参数名与形参对象属性名相同,按照对象层次结构关系即可接收嵌套 POJO 属性参数。
发送请求和参数:
后台接收参数:
//POJO参数:请求参数与形参对象中的属性对应即可完成参数传递
@RequestMapping("/pojoParam")
@ResponseBody
public String pojoParam(User user){
System.out.println("pojo参数传递 user ==> "+user);
return "{'module':'pojo param'}";
}
请求参数 key 的名称要和 POJO 中属性的名称一致,否则无法封装。
4.3.4 数组类型参数
举个简单的例子,如果前端需要获取用户的爱好,爱好绝大多数情况下都是多个,如何发送请求数据和接收数据呢?
- 数组参数:请求参数名与形参对象属性名相同且请求参数为多个,定义数组类型即可接收参数。
发送请求和参数:
后台接收参数:
//数组参数:同名请求参数可以直接映射到对应名称的形参数组对象中
@RequestMapping("/arrayParam")
@ResponseBody
public String arrayParam(String[] likes){
System.out.println("数组参数传递 likes ==> "+ Arrays.toString(likes));
return "{'module':'array param'}";
}
4.3.5 集合类型参数
数组能接收多个值,那么集合是否也可以实现这个功能呢?
发送请求和参数:
后台接收参数:
//集合参数:同名请求参数可以使用@RequestParam注解映射到对应名称的集合对象中作为数据
@RequestMapping("/listParam")
@ResponseBody
public String listParam(List<String> likes){
System.out.println("集合参数传递 likes ==> "+ likes);
return "{'module':'list param'}";
}
运行会报错。
错误的原因是:SpringMVC 将 List 看做是一个 POJO 对象来处理,将其创建一个对象并准备把前端的数据封装到对象中,但是 List 是一个接口无法创建对象,所以报错。
解决方案是:使用 @RequestParam 注解。
//集合参数:同名请求参数可以使用@RequestParam注解映射到对应名称的集合对象中作为数据
@RequestMapping("/listParam")
@ResponseBody
public String listParam(@RequestParam List<String> likes){
System.out.println("集合参数传递 likes ==> "+ likes);
return "{'module':'list param'}";
}
- 集合保存普通参数:请求参数名与形参集合对象名相同且请求参数为多个,@RequestParam 绑定参数关系。
- 对于简单数据类型使用数组会比集合更简单些。
知识点:@RequestParam
| 名称 | @RequestParam |
|---|---|
| 类型 | 形参注解 |
| 位置 | SpringMVC 控制器方法形参定义前面 |
| 作用 | 绑定请求参数与处理器方法形参间的关系 |
| 相关参数 | required:是否为必传参数 defaultValue:参数默认值 |
4.4 JSON 数据传输参数
前面我们说过,现在比较流行的开发方式为异步调用。前后台以异步方式进行交换,传输的数据使用的是 JSON,所以前端如果发送的是 JSON 数据,后端该如何接收?
对于 JSON 数据类型,我们常见的有三种:
- json 普通数组([“value1”,”value2”,”value3”,…])
- json 对象({key1:value1,key2:value2,…})
- json 对象数组([{key1:value1,…},{key2:value2,…}])
对于上述数据,前端如何发送,后端如何接收?
JSON 普通数组
步骤1:pom.xml 添加依赖
SpringMVC 默认使用的是 jackson 来处理 json 的转换,所以需要在 pom.xml 添加 jackson 依赖。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
步骤2:PostMan 发送 JSON 数据
步骤3:开启 SpringMVC 注解支持
在 SpringMVC 的配置类中开启 SpringMVC 的注解支持,这里面就包含了将 JSON 转换成对象的功能。
@Configuration
@ComponentScan("com.itheima.controller")
//开启json数据类型自动转换
@EnableWebMvc
public class SpringMvcConfig {
}
步骤4:参数前添加 @RequestBody
//使用@RequestBody注解将外部传递的json数组数据映射到形参的集合对象中作为数据
@RequestMapping("/listParamForJson")
@ResponseBody
public String listParamForJson(@RequestBody List<String> likes){
System.out.println("list common(json)参数传递 list ==> "+likes);
return "{'module':'list common for json param'}";
}
步骤5:启动运行程序
JSON 普通数组的数据就已经传递完成,下面针对 JSON 对象数据和 JSON 对象数组的数据该如何传递呢?
JSON 对象数据
我们会发现,只需要关注请求和数据如何发送?后端数据如何接收?
请求和数据的发送:
{
"name":"itcast",
"age":15
}
后端接收数据:
@RequestMapping("/pojoParamForJson")
@ResponseBody
public String pojoParamForJson(@RequestBody User user){
System.out.println("pojo(json)参数传递 user ==> "+user);
return "{'module':'pojo for json param'}";
}
启动程序访问测试。
说明:
address 为 null 的原因是前端没有传递数据给后端。
如果想要 address 也有数据,我们需要修改前端传递的数据内容:
{
"name":"itcast",
"age":15,
"address":{
"province":"beijing",
"city":"beijing"
}
}
再次发送请求,就能看到 address 中的数据。
JSON 对象数组
集合中保存多个 POJO 该如何实现?
请求和数据的发送:
[
{"name":"itcast","age":15},
{"name":"itheima","age":12}
]
后端接收数据:
@RequestMapping("/listPojoParamForJson")
@ResponseBody
public String listPojoParamForJson(@RequestBody List<User> list){
System.out.println("list pojo(json)参数传递 list ==> "+list);
return "{'module':'list pojo for json param'}";
}
启动程序访问测试。
SpringMVC 接收 JSON 数据的实现步骤为:
(1) 导入 jackson 包。
(2) 使用 PostMan 发送 JSON 数据。
(3) 开启 SpringMVC 注解驱动,在配置类上添加 @EnableWebMvc 注解。
(4) Controller 方法的参数前添加 @RequestBody 注解。
知识点:@EnableWebMvc
| 名称 | @EnableWebMvc |
|---|---|
| 类型 | 配置类注解 |
| 位置 | SpringMVC 配置类定义上方 |
| 作用 | 开启 SpringMVC 多项辅助功能 |
知识点:@RequestBody
| 名称 | @RequestBody |
|---|---|
| 类型 | 形参注解 |
| 位置 | SpringMVC 控制器方法形参定义前面 |
| 作用 | 将请求中请求体所包含的数据传递给请求参数,此注解一个处理器方法只能使用一次 |
@RequestBody 与 @RequestParam 区别
| 注解 | 适用场景 | 数据格式 |
|---|---|---|
| @RequestParam | 接收 url 地址传参,表单传参 | application/x-www-form-urlencoded |
| @RequestBody | 接收 json 数据 | application/json |
- 应用:
- 后期开发中,发送 json 格式数据为主,@RequestBody 应用较广。
- 如果发送非 json 格式数据,选用 @RequestParam 接收请求参数。
4.5 日期类型参数传递
前面我们处理过简单数据类型、POJO 数据类型、数组和集合数据类型以及 JSON 数据类型,接下来我们还得处理一种开发中比较常见的一种数据类型,日期类型。
日期类型比较特殊,因为对于日期的格式有 N 多种输入方式,比如:
- 2088-08-18
- 2088/08/18
- 08/18/2088
- ……
针对这么多日期格式,SpringMVC 该如何接收,它能很好的处理日期类型数据么?
步骤1:编写方法接收日期数据
在 UserController 类中添加方法,把参数设置为日期类型。
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date)
System.out.println("参数传递 date ==> "+date);
return "{'module':'data param'}";
}
步骤2:启动 Tomcat 服务器
查看控制台是否报错,如果有错误,先解决错误。
步骤3:使用 PostMan 发送请求
使用 PostMan 发送 GET 请求,并设置 date 参数。
http://localhost/dataParam?date=2088/08/08
步骤4:查看控制台
通过打印,我们发现 SpringMVC 可以接收日期数据类型,并将其打印在控制台。
这个时候,我们就想如果把日期参数的格式改成其他的,SpringMVC 还能处理么?
步骤5:更换日期格式
为了能更好的看到程序运行的结果,我们在方法中多添加一个日期参数。
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date,Date date1)
System.out.println("参数传递 date ==> "+date);
return "{'module':'data param'}";
}
使用 PostMan 发送请求,携带两个不同的日期格式:
http://localhost/dataParam?date=2088/08/08&date1=2088-08-08
发送请求和数据后,页面会报 400,控制台会报出一个错误:
Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type ‘java.lang.String’ to required type ‘java.util.Date’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value ‘2088-08-08’; nested exception is java.lang.IllegalArgumentException]
从错误信息可以看出,错误的原因是在将 2088-08-08 转换成日期类型的时候失败了,原因是 SpringMVC 默认支持的字符串转日期的格式为 yyyy/MM/dd,而我们现在传递的不符合其默认格式,SpringMVC 就无法进行格式转换,所以报错。
解决方案也比较简单,需要使用 @DateTimeFormat。
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date,
@DateTimeFormat(pattern="yyyy-MM-dd") Date date1)
System.out.println("参数传递 date ==> "+date);
System.out.println("参数传递 date1(yyyy-MM-dd) ==> "+date1);
return "{'module':'data param'}";
}
重新启动服务器,重新发送请求测试,SpringMVC 就可以正确的进行日期转换了。
步骤6:携带时间的日期
接下来我们再来发送一个携带时间的日期,看下 SpringMVC 该如何处理?
先修改 UserController 类,添加第三个参数。
@RequestMapping("/dataParam")
@ResponseBody
public String dataParam(Date date,
@DateTimeFormat(pattern="yyyy-MM-dd") Date date1,
@DateTimeFormat(pattern="yyyy/MM/dd HH:mm:ss") Date date2)
System.out.println("参数传递 date ==> "+date);
System.out.println("参数传递 date1(yyyy-MM-dd) ==> "+date1);
System.out.println("参数传递 date2(yyyy/MM/dd HH:mm:ss) ==> "+date2);
return "{'module':'data param'}";
}
使用 PostMan 发送请求,携带两个不同的日期格式:
http://localhost/dataParam?date=2088/08/08&date1=2088-08-08&date2=2088/08/08 8:08:08
重新启动服务器,重新发送请求测试,SpringMVC 就可以将日期时间的数据进行转换。
知识点:@DateTimeFormat
| 名称 | @DateTimeFormat |
|---|---|
| 类型 | 形参注解 |
| 位置 | SpringMVC 控制器方法形参前面 |
| 作用 | 设定日期时间型数据格式 |
| 相关属性 | pattern:指定日期时间格式字符串 |
内部实现原理
讲解内部原理之前,我们需要先思考个问题:
- 前端传递字符串,后端使用日期 Date 接收。
- 前端传递 JSON 数据,后端使用对象接收。
- 前端传递字符串,后端使用 Integer 接收。
- 后台需要的数据类型有很多中。
- 在数据的传递过程中存在很多类型的转换。
问:谁来做这个类型转换?答:SpringMVC。
问:SpringMVC 是如何实现类型转换的?答:SpringMVC 中提供了很多类型转换接口和实现类。
在框架中,有一些类型转换接口,其中有:
- (1) Converter 接口。
/**
* S: the source type
* T: the target type
*/
public interface Converter<S, T> {
@Nullable
//该方法就是将从页面上接收的数据(S)转换成我们想要的数据类型(T)返回
T convert(S source);
}
注意:Converter 所属的包为 org.springframework.core.convert.converter。
Converter 接口的实现类:
框架中有提供很多对应 Converter 接口的实现类,用来实现不同数据类型之间的转换,如:
请求参数年龄数据(String→Integer)
日期格式转换(String → Date)
(2) HttpMessageConverter 接口。
该接口是实现对象与 JSON 之间的转换工作。
SpringMVC 的配置类把 @EnableWebMvc 当做标配配置上去,不要省略。
4.6 响应
SpringMVC 接收到请求和数据后,进行一些了的处理,当然这个处理可以是转发给 Service,Service 层再调用 Dao 层完成的,不管怎样,处理完以后,都需要将结果告知给用户。
比如:根据用户 ID 查询用户信息、查询用户列表、新增用户等。
对于响应,主要就包含两部分内容:
- 响应页面
- 响应数据
- 文本数据
- json 数据
因为异步调用是目前常用的主流方式,所以我们需要更关注的就是如何返回 JSON 数据,对于其他只需要认识了解即可。
4.6.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 Spring 依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_05_response</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建对应的配置类。
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
@Configuration
@ComponentScan("com.itheima.controller")
//开启json数据类型自动转换
@EnableWebMvc
public class SpringMvcConfig {
}
- 编写模型类 User。
public class User {
private String name;
private int age;
//getter...setter...toString省略
}
- webapp 下创建 page.jsp。
<html>
<body>
<h2>Hello Spring MVC!</h2>
</body>
</html>
- 编写 UserController。
@Controller
public class UserController {
}
最终创建好的项目结构如下:
4.6.2 响应页面[了解]
步骤1:设置返回页面
@Controller
public class UserController {
@RequestMapping("/toJumpPage")
//注意
//1.此处不能添加@ResponseBody,如果加了该注入,会直接将page.jsp当字符串返回前端
//2.方法需要返回String
public String toJumpPage(){
System.out.println("跳转页面");
return "page.jsp";
}
}
步骤2:启动程序测试
此处涉及到页面跳转,所以不适合采用 PostMan 进行测试,直接打开浏览器,输入:
http://localhost/toJumpPage
4.6.3 返回文本数据[了解]
步骤1:设置返回文本内容
@Controller
public class UserController {
@RequestMapping("/toText")
//注意此处该注解就不能省略,如果省略了,会把response text当前页面名称去查找,如果没有回报404错误
@ResponseBody
public String toText(){
System.out.println("返回纯文本数据");
return "response text";
}
}
步骤2:启动程序测试
此处不涉及到页面跳转,因为我们现在发送的是 GET 请求,可以使用浏览器也可以使用 PostMan 进行测试,输入地址 http://localhost/toText 访问。
4.6.4 响应 JSON 数据
响应 POJO 对象
@Controller
public class UserController {
@RequestMapping("/toJsonPOJO")
@ResponseBody
public User toJsonPOJO(){
System.out.println("返回json对象数据");
User user = new User();
user.setName("itcast");
user.setAge(15);
return user;
}
}
返回值为实体类对象,设置返回值为实体类类型,即可实现返回对应对象的 json 数据,需要依赖 @ResponseBody 注解和 @EnableWebMvc 注解。
重新启动服务器,访问 http://localhost/toJsonPOJO。
响应 POJO 集合对象
@Controller
public class UserController {
@RequestMapping("/toJsonList")
@ResponseBody
public List<User> toJsonList(){
System.out.println("返回json集合数据");
User user1 = new User();
user1.setName("传智播客");
user1.setAge(15);
User user2 = new User();
user2.setName("黑马程序员");
user2.setAge(12);
List<User> userList = new ArrayList<User>();
userList.add(user1);
userList.add(user2);
return userList;
}
}
重新启动服务器,访问 http://localhost/toJsonList。
知识点:@ResponseBody
| 名称 | @ResponseBody |
|---|---|
| 类型 | 方法\类注解 |
| 位置 | SpringMVC 控制器方法定义上方和控制类上 |
| 作用 | 设置当前控制器返回值作为响应体, 写在类上,该类的所有方法都有该注解功能 |
| 相关属性 | pattern:指定日期时间格式字符串 |
说明:
- 该注解可以写在类上或者方法上。
- 写在类上就是该类下的所有方法都有 @ReponseBody 功能。
- 当方法上有 @ReponseBody 注解后:
- 方法的返回值为字符串,会将其作为文本内容直接响应给前端。
- 方法的返回值为对象,会将对象转换成 JSON 响应给前端。
此处又使用到了类型转换,内部还是通过 Converter 接口的实现类完成的,所以 Converter 除了前面所说的功能外,它还可以实现:
- 对象转 Json 数据(POJO -> json)
- 集合转 Json 数据(Collection -> json)
5. Rest 风格
对于 Rest 风格,我们需要学习的内容包括:
- REST 简介
- REST 入门案例
- REST 快速开发
- 案例:基于 RESTful 页面数据交互
5.1 REST 简介
REST(Representational State Transfer),表现形式状态转换,它是一种软件架构风格。
当我们想表示一个网络资源的时候,可以使用两种方式:
- 传统风格资源描述形式
http://localhost/user/getById?id=1查询 id 为 1 的用户信息http://localhost/user/saveUser保存用户信息
- REST 风格描述形式
http://localhost/user/1http://localhost/user
- 传统风格资源描述形式
传统方式一般是一个请求 url 对应一种操作,这样做不仅麻烦,也不安全,因为会程序的人读取了你的请求 url 地址,就大概知道该 url 实现的是一个什么样的操作。
查看 REST 风格的描述,你会发现请求地址变的简单了,并且光看请求 URL 并不是很能猜出来该 URL 的具体功能。
所以 REST 的优点有:
- 隐藏资源的访问行为,无法通过地址得知对资源是何种操作。
- 书写简化。
但是我们的问题也随之而来了,一个相同的 url 地址既可以是新增也可以是修改或者查询,那么到底我们该如何区分该请求到底是什么操作呢?
- 按照 REST 风格访问资源时使用行为动作区分对资源进行了何种操作。
http://localhost/users查询全部用户信息 GET(查询)http://localhost/users/1查询指定用户信息 GET(查询)http://localhost/users添加用户信息 POST(新增/保存)http://localhost/users修改用户信息 PUT(修改/更新)http://localhost/users/1删除用户信息 DELETE(删除)
请求的方式比较多,但是比较常用的就 4 种,分别是 GET、POST、PUT、DELETE。
按照不同的请求方式代表不同的操作类型。
- 发送 GET 请求是用来做查询。
- 发送 POST 请求是用来做新增。
- 发送 PUT 请求是用来做修改。
- 发送 DELETE 请求是用来做删除。
- 上述行为是约定方式,约定不是规范,可以打破,所以称 REST 风格,而不是 REST 规范。
- REST 提供了对应的架构方式,按照这种架构设计项目可以降低开发的复杂性,提高系统的可伸缩性。
- REST 中规定 GET/POST/PUT/DELETE 针对的是查询/新增/修改/删除,但是我们如果非要用 GET 请求做删除,这点在程序上运行是可以实现的。
- 但是如果绝大多数人都遵循这种风格,你写的代码让别人读起来就有点莫名其妙了。
- 描述模块的名称通常使用复数,也就是加 s 的格式描述,表示此类资源,而非单个资源,例如:users、books、accounts……
清楚了什么是 REST 风格后,我们后期会经常提到一个概念叫 RESTful,那什么又是 RESTful 呢?
- 根据 REST 风格对资源进行访问称为 RESTful。
后期我们在进行开发的过程中,大多是都是遵从 REST 风格来访问我们的后台服务,所以可以说咱们以后都是基于 RESTful 来进行开发的。
5.2 RESTful 入门案例
5.2.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 Spring 依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_06_rest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建对应的配置类。
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
@Configuration
@ComponentScan("com.itheima.controller")
//开启json数据类型自动转换
@EnableWebMvc
public class SpringMvcConfig {
}
- 编写模型类 User 和 Book。
public class User {
private String name;
private int age;
//getter...setter...toString省略
}
public class Book {
private String name;
private double price;
//getter...setter...toString省略
}
- 编写 UserController 和 BookController。
@Controller
public class UserController {
@RequestMapping("/save")
@ResponseBody
public String save(@RequestBody User user) {
System.out.println("user save..."+user);
return "{'module':'user save'}";
}
@RequestMapping("/delete")
@ResponseBody
public String delete(Integer id) {
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
@RequestMapping("/update")
@ResponseBody
public String update(@RequestBody User user) {
System.out.println("user update..." + user);
return "{'module':'user update'}";
}
@RequestMapping("/getById")
@ResponseBody
public String getById(Integer id) {
System.out.println("user getById..." + id);
return "{'module':'user getById'}";
}
@RequestMapping("/findAll")
@ResponseBody
public String getAll() {
System.out.println("user getAll...");
return "{'module':'user getAll'}";
}
}
@Controller
public class BookController {
@RequestMapping(value = "/books",method = RequestMethod.POST)
@ResponseBody
public String save(@RequestBody Book book){
System.out.println("book save..." + book);
return "{'module':'book save'}";
}
@RequestMapping(value = "/books/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id){
System.out.println("book delete..." + id);
return "{'module':'book delete'}";
}
@RequestMapping(value = "/books",method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody Book book){
System.out.println("book update..." + book);
return "{'module':'book update'}";
}
@RequestMapping(value = "/books/{id}",method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id){
System.out.println("book getById..." + id);
return "{'module':'book getById'}";
}
@RequestMapping(value = "/books",method = RequestMethod.GET)
@ResponseBody
public String getAll(){
System.out.println("book getAll...");
return "{'module':'book getAll'}";
}
}
最终创建好的项目结构如下:
5.2.2 思路分析
需求:将之前的增删改查替换成 RESTful 的开发方式。
之前不同的请求有不同的路径,现在要将其修改为统一的请求路径。
修改前:新增: /save ,修改: /update,删除 /delete…
修改后:增删改查: /users
根据 GET 查询、POST 新增、PUT 修改、DELETE 删除对方法的请求方式进行限定。
发送请求的过程中如何设置请求参数?
5.2.3 修改 RESTful 风格
新增
@Controller
public class UserController {
//设置当前请求方法为POST,表示REST风格中的添加操作
@RequestMapping(value = "/users",method = RequestMethod.POST)
@ResponseBody
public String save() {
System.out.println("user save...");
return "{'module':'user save'}";
}
}
- 将请求路径更改为
/users。- 访问该方法使用 POST:
http://localhost/users。
- 访问该方法使用 POST:
- 使用 method 属性限定该方法的访问方式为
POST。- 如果发送的不是 POST 请求,比如发送 GET 请求,则会报错。
删除
@Controller
public class UserController {
//设置当前请求方法为DELETE,表示REST风格中的删除操作
@RequestMapping(value = "/users",method = RequestMethod.DELETE)
@ResponseBody
public String delete(Integer id) {
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
}
- 将请求路径更改为
/users。- 访问该方法使用 DELETE:
http://localhost/users。
- 访问该方法使用 DELETE:
访问成功,但是删除方法没有携带所要删除数据的 id,所以针对 RESTful 的开发,如何携带数据参数?
传递路径参数
前端发送请求的时候使用:http://localhost/users/1,路径中的 1 就是我们想要传递的参数。
后端获取参数,需要做如下修改:
- 修改 @RequestMapping 的 value 属性,将其中修改为
/users/{id},目的是和路径匹配。 - 在方法的形参前添加 @PathVariable 注解。
@Controller
public class UserController {
//设置当前请求方法为DELETE,表示REST风格中的删除操作
@RequestMapping(value = "/users/{id}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id) {
System.out.println("user delete..." + id);
return "{'module':'user delete'}";
}
}
思考如下两个问题:
(1) 如果方法形参的名称和路径 {} 中的值不一致,该怎么办?
(2) 如果有多个参数需要传递该如何编写?
前端发送请求的时候使用:http://localhost/users/1/tom,路径中的 1 和 tom 就是我们想要传递的两个参数。
后端获取参数,需要做如下修改:
@Controller
public class UserController {
//设置当前请求方法为DELETE,表示REST风格中的删除操作
@RequestMapping(value = "/users/{id}/{name}",method = RequestMethod.DELETE)
@ResponseBody
public String delete(@PathVariable Integer id,@PathVariable String name) {
System.out.println("user delete..." + id+","+name);
return "{'module':'user delete'}";
}
}
修改
@Controller
public class UserController {
//设置当前请求方法为PUT,表示REST风格中的修改操作
@RequestMapping(value = "/users",method = RequestMethod.PUT)
@ResponseBody
public String update(@RequestBody User user) {
System.out.println("user update..." + user);
return "{'module':'user update'}";
}
}
- 将请求路径更改为
/users。- 访问该方法使用 PUT:
http://localhost/users。
- 访问该方法使用 PUT:
根据 ID 查询
@Controller
public class UserController {
//设置当前请求方法为GET,表示REST风格中的查询操作
@RequestMapping(value = "/users/{id}" ,method = RequestMethod.GET)
@ResponseBody
public String getById(@PathVariable Integer id){
System.out.println("user getById..."+id);
return "{'module':'user getById'}";
}
}
将请求路径更改为 /users。
- 访问该方法使用 GET:
http://localhost/users/666。
查询所有
@Controller
public class UserController {
//设置当前请求方法为GET,表示REST风格中的查询操作
@RequestMapping(value = "/users" ,method = RequestMethod.GET)
@ResponseBody
public String getAll() {
System.out.println("user getAll...");
return "{'module':'user getAll'}";
}
}
将请求路径更改为 /users。
- 访问该方法使用 GET:
http://localhost/users。
RESTful 入门案例,我们需要学习的内容如下:
(1) 设定 Http 请求动作(动词):@RequestMapping(value="", method = RequestMethod.POST|GET|PUT|DELETE)。
(2) 设定请求参数(路径变量):@RequestMapping(value="/users/{id}",method = RequestMethod.DELETE),配合 @PathVariable Integer id 使用。
知识点:@PathVariable
| 名称 | @PathVariable |
|---|---|
| 类型 | 形参注解 |
| 位置 | SpringMVC 控制器方法形参定义前面 |
| 作用 | 绑定路径参数与处理器方法形参间的关系,要求路径参数名与形参名一一对应 |
关于接收参数,我们学过三个注解 @RequestBody、@RequestParam、@PathVariable,这三个注解之间的区别和应用分别是什么?
| 注解 | 适用场景 | 说明 |
|---|---|---|
| @RequestParam | 接收 url 地址传参或表单传参 | 适用于非 json 格式数据 |
| @RequestBody | 接收 json 数据 | 适用于 json 格式数据 |
| @PathVariable | 接收路径参数,使用 {参数名称} 描述路径参数 | 适用于 RESTful 路径变量,通常用于传递 id 值 |
- 应用:
- 后期开发中,发送请求参数超过 1 个时,以 json 格式为主,@RequestBody 应用较广。
- 如果发送非 json 格式数据,选用 @RequestParam 接收请求参数。
- 采用 RESTful 进行开发,当参数数量较少时,例如 1 个,可以采用 @PathVariable 接收请求路径变量,通常用于传递 id 值。
5.3 RESTful 快速开发
做完了 RESTful 的开发,你会发现好麻烦,麻烦在哪?
问题1:每个方法的 @RequestMapping 注解中都定义了访问路径 /books,重复性太高。
问题2:每个方法的 @RequestMapping 注解中都要使用 method 属性定义请求方式,重复性太高。
问题3:每个方法响应 json 都需要加上 @ResponseBody 注解,重复性太高。
对于上面所提的这三个问题,具体该如何解决?
@RestController //@Controller + ReponseBody
@RequestMapping("/books")
public class BookController {
//@RequestMapping(method = RequestMethod.POST)
@PostMapping
public String save(@RequestBody Book book){
System.out.println("book save..." + book);
return "{'module':'book save'}";
}
//@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){
System.out.println("book delete..." + id);
return "{'module':'book delete'}";
}
//@RequestMapping(method = RequestMethod.PUT)
@PutMapping
public String update(@RequestBody Book book){
System.out.println("book update..." + book);
return "{'module':'book update'}";
}
//@RequestMapping(value = "/{id}",method = RequestMethod.GET)
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("book getById..." + id);
return "{'module':'book getById'}";
}
//@RequestMapping(method = RequestMethod.GET)
@GetMapping
public String getAll(){
System.out.println("book getAll...");
return "{'module':'book getAll'}";
}
}
对于刚才的问题,我们都有对应的解决方案:
问题1:每个方法的 @RequestMapping 注解中都定义了访问路径 /books,重复性太高。
将@RequestMapping提到类上面,用来定义所有方法共同的访问路径。
问题2:每个方法的 @RequestMapping 注解中都要使用 method 属性定义请求方式,重复性太高。
使用@GetMapping @PostMapping @PutMapping @DeleteMapping代替
问题3:每个方法响应 json 都需要加上 @ResponseBody 注解,重复性太高。
1.将ResponseBody提到类上面,让所有的方法都有@ResponseBody的功能
2.使用@RestController注解替换@Controller与@ResponseBody注解,简化书写
知识点:@RestController
| 名称 | @RestController |
|---|---|
| 类型 | 类注解 |
| 位置 | 基于 SpringMVC 的 RESTful 开发控制器类定义上方 |
| 作用 | 设置当前控制器类为 RESTful 风格, 等同于 @Controller 与 @ResponseBody 两个注解组合功能 |
知识点:@GetMapping @PostMapping @PutMapping @DeleteMapping
| 名称 | @GetMapping @PostMapping @PutMapping @DeleteMapping |
|---|---|
| 类型 | 方法注解 |
| 位置 | 基于 SpringMVC 的 RESTful 开发控制器方法定义上方 |
| 作用 | 设置当前控制器方法请求访问路径与请求动作,每种对应一个请求动作, 例如 @GetMapping 对应 GET 请求 |
| 相关属性 | value(默认):请求访问路径 |
5.4 RESTful 案例
5.4.1 需求分析
需求一:图片列表查询,从后台返回数据,将数据展示在页面上。
需求二:新增图片,将新增图书的数据传递到后台,并在控制台打印。
说明:此次案例的重点是在 SpringMVC 中如何使用 RESTful 实现前后台交互,所以本案例并没有和数据库进行交互,所有数据使用假数据来完成开发。
步骤分析:
- 搭建项目导入 jar 包。
- 编写 Controller 类,提供两个方法,一个用来做列表查询,一个用来做新增。
- 在方法上使用 RESTful 进行路径设置。
- 完成请求、参数的接收和结果的响应。
- 使用 PostMan 进行测试。
- 将前端页面拷贝到项目中。
- 页面发送 ajax 请求。
- 完成页面数据的展示。
5.4.2 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 Spring 依赖。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_07_rest_case</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建对应的配置类。
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
@Configuration
@ComponentScan("com.itheima.controller")
//开启json数据类型自动转换
@EnableWebMvc
public class SpringMvcConfig {
}
- 编写模型类 Book。
public class Book {
private Integer id;
private String type;
private String name;
private String description;
//setter...getter...toString略
}
- 编写 BookController。
@Controller
public class BookController {
}
最终创建好的项目结构如下:
5.4.3 后台接口开发
步骤1:编写 Controller 类并使用 RESTful 进行配置
@RestController
@RequestMapping("/books")
public class BookController {
@PostMapping
public String save(@RequestBody Book book){
System.out.println("book save ==> "+ book);
return "{'module':'book save success'}";
}
@GetMapping
public List<Book> getAll(){
System.out.println("book getAll is running ...");
List<Book> bookList = new ArrayList<Book>();
Book book1 = new Book();
book1.setType("计算机");
book1.setName("SpringMVC入门教程");
book1.setDescription("小试牛刀");
bookList.add(book1);
Book book2 = new Book();
book2.setType("计算机");
book2.setName("SpringMVC实战教程");
book2.setDescription("一代宗师");
bookList.add(book2);
Book book3 = new Book();
book3.setType("计算机丛书");
book3.setName("SpringMVC实战教程进阶");
book3.setDescription("一代宗师呕心创作");
bookList.add(book3);
return bookList;
}
}
步骤2:使用 PostMan 进行测试
测试新增:
{
"type":"计算机丛书",
"name":"SpringMVC终极开发",
"description":"这是一本好书"
}
测试查询:

5.4.4 页面访问处理
步骤1:拷贝静态页面
将 资料\功能页面 下的所有内容拷贝到项目的 webapp 目录下。
步骤2:访问 pages 目录下的 books.html
打开浏览器输入 http://localhost/pages/books.html。
(1) 出现错误的原因?
SpringMVC 拦截了静态资源,根据 /pages/books.html 去 controller 找对应的方法,找不到所以会报 404 的错误。
(2) SpringMVC 为什么会拦截静态资源呢?
(3) 解决方案?
- SpringMVC 需要将静态资源进行放行。
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
//设置静态资源访问过滤,当前类需要设置为配置类,并被扫描加载
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//当访问/pages/????时候,从/pages目录下查找内容
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
- 该配置类是在 config 目录下,SpringMVC 扫描的是 controller 包,所以该配置类还未生效,要想生效需要将 SpringMvcConfig 配置类进行修改。
@Configuration
@ComponentScan({"com.itheima.controller","com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
或者
@Configuration
@ComponentScan("com.itheima")
@EnableWebMvc
public class SpringMvcConfig {
}
步骤3:修改 books.html 页面
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<title>SpringMVC案例</title>
<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
<div class="content-header">
<h1>图书管理</h1>
</div>
<div class="app-container">
<div class="box">
<div class="filter-container">
<el-input placeholder="图书名称" style="width: 200px;" class="filter-item"></el-input>
<el-button class="dalfBut">查询</el-button>
<el-button type="primary" class="butT" @click="openSave()">新建</el-button>
</div>
<el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
<el-table-column type="index" align="center" label="序号"></el-table-column>
<el-table-column prop="type" label="图书类别" align="center"></el-table-column>
<el-table-column prop="name" label="图书名称" align="center"></el-table-column>
<el-table-column prop="description" label="描述" align="center"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button type="primary" size="mini">编辑</el-button>
<el-button size="mini" type="danger">删除</el-button>
</template>
</el-table-column>
</el-table>
<div class="pagination-container">
<el-pagination
class="pagiantion"
@current-change="handleCurrentChange"
:current-page="pagination.currentPage"
:page-size="pagination.pageSize"
layout="total, prev, pager, next, jumper"
:total="pagination.total">
</el-pagination>
</div>
<!-- 新增标签弹层 -->
<div class="add-form">
<el-dialog title="新增图书" :visible.sync="dialogFormVisible">
<el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="图书类别" prop="type">
<el-input v-model="formData.type"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="图书名称" prop="name">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="描述">
<el-input v-model="formData.description" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="saveBook()">确定</el-button>
</div>
</el-dialog>
</div>
</div>
</div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
var vue = new Vue({
el: '#app',
data:{
dataList: [],//当前页要展示的分页列表数据
formData: {},//表单数据
dialogFormVisible: false,//增加表单是否可见
dialogFormVisible4Edit:false,//编辑表单是否可见
pagination: {},//分页模型数据,暂时弃用
},
//钩子函数,VUE对象初始化完成后自动执行
created() {
this.getAll();
},
methods: {
// 重置表单
resetForm() {
//清空输入框
this.formData = {};
},
// 弹出添加窗口
openSave() {
this.dialogFormVisible = true;
this.resetForm();
},
//添加
saveBook () {
axios.post("/books",this.formData).then((res)=>{
});
},
//主页列表查询
getAll() {
axios.get("/books").then((res)=>{
this.dataList = res.data;
});
},
}
})
</script>
</html>
6. SSM 整合
前面我们已经把 Mybatis、Spring 和 SpringMVC 三个框架进行了学习,今天主要的内容就是把这三个框架整合在一起完成我们的业务功能开发,具体如何来整合,我们一步步来学习。
6.1 流程分析
(1) 创建工程
- 创建一个 Maven 的 web 工程。
- pom.xml 添加 SSM 需要的依赖 jar 包。
- 编写 Web 项目的入口配置类,实现
AbstractAnnotationConfigDispatcherServletInitializer重写以下方法。- getRootConfigClasses():返回 Spring 的配置类 -> 需要 SpringConfig 配置类。
- getServletConfigClasses():返回 SpringMVC 的配置类 -> 需要 SpringMvcConfig 配置类。
- getServletMappings():设置 SpringMVC 请求拦截路径规则。
- getServletFilters():设置过滤器,解决 POST 请求中文乱码问题。
(2) SSM 整合
- SpringConfig
- 标识该类为配置类 @Configuration。
- 扫描 Service 所在的包 @ComponentScan。
- 在 Service 层要管理事务 @EnableTransactionManagement。
- 读取外部的 properties 配置文件 @PropertySource。
- 整合 Mybatis 需要引入 Mybatis 相关配置类 @Import。
- 第三方数据源配置类 JdbcConfig。
- 构建 DataSource 数据源,DruidDataSouroce,需要注入数据库连接四要素,@Bean @Value。
- 构建平台事务管理器,DataSourceTransactionManager,@Bean。
- Mybatis 配置类 MybatisConfig。
- 构建 SqlSessionFactoryBean 并设置别名扫描与数据源,@Bean。
- 构建 MapperScannerConfigurer 并设置 DAO 层的包扫描。
- 第三方数据源配置类 JdbcConfig。
- SpringMvcConfig
- 标识该类为配置类 @Configuration。
- 扫描 Controller 所在的包 @ComponentScan。
- 开启 SpringMVC 注解支持 @EnableWebMvc。
(3) 功能模块[与具体的业务模块有关]
- 创建数据库表。
- 根据数据库表创建对应的模型类。
- 通过 Dao 层完成数据库表的增删改查(接口+自动代理)。
- 编写 Service 层[Service 接口+实现类]。
- @Service。
- @Transactional。
- 整合 Junit 对业务层进行单元测试。
- @RunWith。
- @ContextConfiguration。
- @Test。
- 编写 Controller 层。
- 接收请求 @RequestMapping @GetMapping @PostMapping @PutMapping @DeleteMapping。
- 接收数据 简单、POJO、嵌套 POJO、集合、数组、JSON 数据类型。
- @RequestParam。
- @PathVariable。
- @RequestBody。
- 转发业务层。
- @Autowired。
- 响应结果。
- @ResponseBody。
6.2 整合配置
掌握上述的知识点后,接下来,我们就可以按照上述的步骤一步步的来完成 SSM 的整合。
步骤1:创建 Maven 的 web 项目
可以使用 Maven 的骨架创建。
步骤2:添加依赖
pom.xml 添加 SSM 所需要的依赖 jar 包。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_08_ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
步骤3:创建项目包结构
- config 目录存放的是相关的配置类。
- controller 编写的是 Controller 类。
- dao 存放的是 Dao 接口,因为使用的是 Mapper 接口代理方式,所以没有实现类包。
- service 存的是 Service 接口,impl 存放的是 Service 实现类。
- resources:存入的是配置文件,如 Jdbc.properties。
- webapp:目录可以存放静态资源。
- test/java:存放的是测试类。
步骤4:创建 SpringConfig 配置类
@Configuration
@ComponentScan({"com.itheima.service"})
@PropertySource("classpath:jdbc.properties")
@Import({JdbcConfig.class,MyBatisConfig.class})
@EnableTransactionManagement
public class SpringConfig {
}
步骤5:创建 JdbcConfig 配置类
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource){
DataSourceTransactionManager ds = new DataSourceTransactionManager();
ds.setDataSource(dataSource);
return ds;
}
}
步骤6:创建 MybatisConfig 配置类
public class MyBatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.itheima.domain");
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.itheima.dao");
return msc;
}
}
步骤7:创建 jdbc.properties
在 resources 下提供 jdbc.properties,设置数据库连接四要素。
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm_db
jdbc.username=root
jdbc.password=root
步骤8:创建 SpringMVC 配置类
@Configuration
@ComponentScan("com.itheima.controller")
@EnableWebMvc
public class SpringMvcConfig {
}
步骤9:创建 Web 项目入口配置类
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
//加载Spring配置类
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
//加载SpringMVC配置类
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
//设置SpringMVC请求地址拦截规则
protected String[] getServletMappings() {
return new String[]{"/"};
}
//设置post请求中文乱码过滤器
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("utf-8");
return new Filter[]{filter};
}
}
至此 SSM 整合的环境就已经搭建好了。在这个环境上,我们如何进行功能模块的开发呢?
6.3 功能模块开发
需求:对表 tbl_book 进行新增、修改、删除、根据 ID 查询和查询所有。
步骤1:创建数据库及表
create database ssm_db character set utf8;
use ssm_db;
create table tbl_book(
id int primary key auto_increment,
type varchar(20),
name varchar(50),
description varchar(255)
)
insert into `tbl_book`(`id`,`type`,`name`,`description`) values (1,'计算机理论','Spring实战 第五版','Spring入门经典教程,深入理解Spring原理技术内幕'),(2,'计算机理论','Spring 5核心原理与30个类手写实践','十年沉淀之作,手写Spring精华思想'),(3,'计算机理论','Spring 5设计模式','深入Spring源码刨析Spring源码中蕴含的10大设计模式'),(4,'计算机理论','Spring MVC+Mybatis开发从入门到项目实战','全方位解析面向Web应用的轻量级框架,带你成为Spring MVC开发高手'),(5,'计算机理论','轻量级Java Web企业应用实战','源码级刨析Spring框架,适合已掌握Java基础的读者'),(6,'计算机理论','Java核心技术 卷Ⅰ 基础知识(原书第11版)','Core Java第11版,Jolt大奖获奖作品,针对Java SE9、10、11全面更新'),(7,'计算机理论','深入理解Java虚拟机','5个纬度全面刨析JVM,大厂面试知识点全覆盖'),(8,'计算机理论','Java编程思想(第4版)','Java学习必读经典,殿堂级著作!赢得了全球程序员的广泛赞誉'),(9,'计算机理论','零基础学Java(全彩版)','零基础自学编程的入门图书,由浅入深,详解Java语言的编程思想和核心技术'),(10,'市场营销','直播就这么做:主播高效沟通实战指南','李子柒、李佳奇、薇娅成长为网红的秘密都在书中'),(11,'市场营销','直播销讲实战一本通','和秋叶一起学系列网络营销书籍'),(12,'市场营销','直播带货:淘宝、天猫直播从新手到高手','一本教你如何玩转直播的书,10堂课轻松实现带货月入3W+');
步骤2:编写模型类
public class Book {
private Integer id;
private String type;
private String name;
private String description;
//getter...setter...toString省略
}
步骤3:编写 Dao 接口
public interface BookDao {
// @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
@Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
public void save(Book book);
@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
public void update(Book book);
@Delete("delete from tbl_book where id = #{id}")
public void delete(Integer id);
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
@Select("select * from tbl_book")
public List<Book> getAll();
}
步骤4:编写 Service 接口和实现类
@Transactional
public interface BookService {
/**
* 保存
* @param book
* @return
*/
public boolean save(Book book);
/**
* 修改
* @param book
* @return
*/
public boolean update(Book book);
/**
* 按id删除
* @param id
* @return
*/
public boolean delete(Integer id);
/**
* 按id查询
* @param id
* @return
*/
public Book getById(Integer id);
/**
* 查询全部
* @return
*/
public List<Book> getAll();
}
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public boolean save(Book book) {
bookDao.save(book);
return true;
}
public boolean update(Book book) {
bookDao.update(book);
return true;
}
public boolean delete(Integer id) {
bookDao.delete(id);
return true;
}
public Book getById(Integer id) {
return bookDao.getById(id);
}
public List<Book> getAll() {
return bookDao.getAll();
}
}
- bookDao 在 Service 中注入的会提示一个红线提示,为什么呢?
- BookDao 是一个接口,没有实现类,接口是不能创建对象的,所以最终注入的应该是代理对象。
- 代理对象是由 Spring 的 IOC 容器来创建管理的。
- IOC 容器又是在 Web 服务器启动的时候才会创建。
- IDEA 在检测依赖关系的时候,没有找到适合的类注入,所以会提示错误提示。
- 但是程序运行的时候,代理对象就会被创建,框架会使用 DI 进行注入,所以程序运行无影响。
- 如何解决上述问题?
- 可以不用理会,因为运行是正常的。
- 设置错误提示级别。
步骤5:编写 Contorller 类
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public boolean save(@RequestBody Book book) {
return bookService.save(book);
}
@PutMapping
public boolean update(@RequestBody Book book) {
return bookService.update(book);
}
@DeleteMapping("/{id}")
public boolean delete(@PathVariable Integer id) {
return bookService.delete(id);
}
@GetMapping("/{id}")
public Book getById(@PathVariable Integer id) {
return bookService.getById(id);
}
@GetMapping
public List<Book> getAll() {
return bookService.getAll();
}
}
对于图书模块的增删改查就已经完成了编写,我们可以从后往前写也可以从前往后写,最终只需要能把功能实现即可。
接下来我们就先把业务层的代码使用 Spring 整合 Junit 的知识点进行单元测试:
6.4 单元测试
步骤1:新建测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {
}
步骤2:注入 Service 类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {
@Autowired
private BookService bookService;
}
步骤3:编写测试方法
我们先来对查询进行单元测试。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class BookServiceTest {
@Autowired
private BookService bookService;
@Test
public void testGetById(){
Book book = bookService.getById(1);
System.out.println(book);
}
@Test
public void testGetAll(){
List<Book> all = bookService.getAll();
System.out.println(all);
}
}
根据 ID 查询,测试的结果为:
查询所有,测试的结果为:
6.5 PostMan 测试
新增
http://localhost/books
{
"type":"类别测试数据",
"name":"书名测试数据",
"description":"描述测试数据"
}
修改
http://localhost/books
{
"id":13,
"type":"类别测试数据",
"name":"书名测试数据",
"description":"描述测试数据"
}
删除
http://localhost/books/14
查询单个
http://localhost/books/1
查询所有
http://localhost/books
7. 统一结果封装
7.1 表现层与前端数据传输协议定义
SSM 整合以及功能模块开发完成后,接下来,我们在上述案例的基础上分析下有哪些问题需要我们去解决下。首先第一个问题是:
- 在 Controller 层增删改返回给前端的是 boolean 类型数据。
- 在 Controller 层查询单个返回给前端的是对象。
- 在 Controller 层查询所有返回给前端的是集合对象。
目前我们就已经有三种数据类型返回给前端,如果随着业务的增长,我们需要返回的数据类型会越来越多。对于前端开发人员在解析数据的时候就比较凌乱了,所以对于前端来说,如果后台能够返回一个统一的数据结果,前端在解析的时候就可以按照一种方式进行解析。开发就会变得更加简单。
所以我们就想能不能将返回结果的数据进行统一,具体如何来做,大体的思路为:
- 为了封装返回的结果数据:创建结果模型类,封装数据到 data 属性中。
- 为了封装返回的数据是何种操作及是否操作成功:封装操作结果到 code 属性中。
- 操作失败后为了封装返回的错误信息:封装特殊消息到 message(msg)属性中。
根据分析,我们可以设置统一数据返回结果类。
public class Result{
private Object data;
private Integer code;
private String msg;
}
Result 类名及类中的字段并不是固定的,可以根据需要自行增减提供若干个构造方法,方便操作。
7.2 表现层与前端数据传输协议实现
前面我们已经分析了如何封装返回结果数据,具体在项目中该如何实现,我们通过个例子来操作一把。
7.2.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 SSM 整合所需 jar 包。
- 创建对应的配置类。
- 编写 Controller、Service 接口、Service 实现类、Dao 接口和模型类。
- resources 下提供 jdbc.properties 配置文件。
因为这个项目环境的内容和 SSM 整合的内容是一致的,所以我们就不在把代码粘出来了,大家在练习的时候可以在前面整合的例子案例环境下,进行本节内容的开发。
最终创建好的项目结构如下:
7.2.2 结果封装
对于结果封装,我们应该是在表现层进行处理,所以我们把结果类放在 controller 包下,当然你也可以放在 domain 包,这个都是可以的,具体如何实现结果封装,具体的步骤为:
步骤1:创建 Result 类
public class Result {
//描述统一格式中的数据
private Object data;
//描述统一格式中的编码,用于区分操作,可以简化配置0或1表示成功失败
private Integer code;
//描述统一格式中的消息,可选属性
private String msg;
public Result() {
}
//构造方法是方便对象的创建
public Result(Integer code,Object data) {
this.data = data;
this.code = code;
}
//构造方法是方便对象的创建
public Result(Integer code, Object data, String msg) {
this.data = data;
this.code = code;
this.msg = msg;
}
//setter...getter...省略
}
步骤2:定义返回码 Code 类
//状态码
public class Code {
public static final Integer SAVE_OK = 20011;
public static final Integer DELETE_OK = 20021;
public static final Integer UPDATE_OK = 20031;
public static final Integer GET_OK = 20041;
public static final Integer SAVE_ERR = 20010;
public static final Integer DELETE_ERR = 20020;
public static final Integer UPDATE_ERR = 20030;
public static final Integer GET_ERR = 20040;
}
code 类中的常量设计也不是固定的,可以根据需要自行增减,例如将查询再进行细分为 GET_OK、GET_ALL_OK、GET_PAGE_OK 等。
步骤3:修改 Controller 类的返回值
//统一每一个控制器方法返回值
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private BookService bookService;
@PostMapping
public Result save(@RequestBody Book book) {
boolean flag = bookService.save(book);
return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
}
@PutMapping
public Result update(@RequestBody Book book) {
boolean flag = bookService.update(book);
return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
boolean flag = bookService.delete(id);
return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
}
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
Book book = bookService.getById(id);
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String msg = book != null ? "" : "数据查询失败,请重试!";
return new Result(code,book,msg);
}
@GetMapping
public Result getAll() {
List<Book> bookList = bookService.getAll();
Integer code = bookList != null ? Code.GET_OK : Code.GET_ERR;
String msg = bookList != null ? "" : "数据查询失败,请重试!";
return new Result(code,bookList,msg);
}
}
步骤4:启动服务测试
至此,我们的返回结果就已经能以一种统一的格式返回给前端。前端根据返回的结果,先从中获取 code,根据 code 判断,如果成功则取 data 属性的值,如果失败,则取 msg 中的值做提示。
8. 统一异常处理
8.1 问题描述
在讲解这一部分知识点之前,我们先来演示个效果,修改 BookController 类的 getById 方法。
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
//手动添加一个错误信息
if(id==1){
int i = 1/0;
}
Book book = bookService.getById(id);
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String msg = book != null ? "" : "数据查询失败,请重试!";
return new Result(code,book,msg);
}
重新启动运行项目,使用 PostMan 发送请求,当传入的 id 为 1,则会出现如下效果:
前端接收到这个信息后和之前我们约定的格式不一致,这个问题该如何解决?
在解决问题之前,我们先来看下异常的种类及出现异常的原因:
- 框架内部抛出的异常:因使用不合规导致。
- 数据层抛出的异常:因外部服务器故障导致(例如:服务器访问超时)。
- 业务层抛出的异常:因业务逻辑书写错误导致(例如:遍历业务书写操作,导致索引异常等)。
- 表现层抛出的异常:因数据收集、校验等规则导致(例如:不匹配的数据类型间导致异常)。
- 工具类抛出的异常:因工具类书写不严谨不够健壮导致(例如:必要释放的连接长期未释放等)。
看完上面这些出现异常的位置,你会发现,在我们开发的任何一个位置都有可能出现异常,而且这些异常是不能避免的。所以我们就得将异常进行处理。
- 各个层级均出现异常,异常处理代码书写在哪一层?—— 所有的异常均抛出到表现层进行处理。
- 异常的种类很多,表现层如何将所有的异常都处理到呢?—— 异常分类。
- 表现层处理异常,每个方法中单独书写,代码书写量巨大且意义不强,如何解决?—— AOP。
对于上面这些问题及解决方案,SpringMVC 已经为我们提供了一套解决方案:
- 异常处理器:集中的、统一的处理项目中出现的异常。
8.2 异常处理器的使用
8.2.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 SSM 整合所需 jar 包。
- 创建对应的配置类。
- 编写 Controller、Service 接口、Service 实现类、Dao 接口和模型类。
- resources 下提供 jdbc.properties 配置文件。
内容参考前面的项目或者直接使用前面的项目进行本节内容的学习。
最终创建好的项目结构如下:
8.2.2 使用步骤
步骤1:创建异常处理器类
//@RestControllerAdvice用于标识当前类为REST风格对应的异常处理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//除了自定义的异常处理器,保留对Exception类型的异常处理,用于处理非预期的异常
@ExceptionHandler(Exception.class)
public void doException(Exception ex){
System.out.println("嘿嘿,异常你哪里跑!")
}
}
确保 SpringMvcConfig 能够扫描到异常处理器类。
步骤2:让程序抛出异常
修改 BookController 的 getById 方法,添加 int i = 1/0。
@GetMapping("/{id}")
public Result getById(@PathVariable Integer id) {
int i = 1/0;
Book book = bookService.getById(id);
Integer code = book != null ? Code.GET_OK : Code.GET_ERR;
String msg = book != null ? "" : "数据查询失败,请重试!";
return new Result(code,book,msg);
}
步骤3:运行程序,测试
说明异常已经被拦截并执行了 doException 方法。
异常处理器类返回结果给前端
//@RestControllerAdvice用于标识当前类为REST风格对应的异常处理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//除了自定义的异常处理器,保留对Exception类型的异常处理,用于处理非预期的异常
@ExceptionHandler(Exception.class)
public Result doException(Exception ex){
System.out.println("嘿嘿,异常你哪里跑!")
return new Result(666,null,"嘿嘿,异常你哪里跑!");
}
}
启动运行程序,测试。
至此,就算后台执行的过程中抛出异常,最终也能按照我们和前端约定好的格式返回给前端。
知识点:@RestControllerAdvice
| 名称 | @RestControllerAdvice |
|---|---|
| 类型 | 类注解 |
| 位置 | Rest 风格开发的控制器增强类定义上方 |
| 作用 | 为 Rest 风格开发的控制器类做增强 |
说明:此注解自带 @ResponseBody 注解与 @Component 注解,具备对应的功能。
知识点:@ExceptionHandler
| 名称 | @ExceptionHandler |
|---|---|
| 类型 | 方法注解 |
| 位置 | 专用于异常处理的控制器方法上方 |
| 作用 | 设置指定异常的处理方案,功能等同于控制器方法, 出现异常后终止原始控制器执行,并转入当前方法执行 |
说明:此类方法可以根据处理的异常不同,制作多个方法分别处理对应的异常。
8.3 项目异常处理方案
8.3.1 异常分类
异常处理器我们已经能够使用了,那么在咱们的项目中该如何来处理异常呢?
因为异常的种类有很多,如果每一个异常都对应一个 @ExceptionHandler,那得写多少个方法来处理各自的异常,所以我们在处理异常之前,需要对异常进行一个分类:
- 业务异常(BusinessException)
- 规范的用户行为产生的异常。
- 用户在页面输入内容的时候未按照指定格式进行数据填写,如在年龄框输入的是字符串。
- 不规范的用户行为操作产生的异常。
- 如用户故意传递错误数据。
- 规范的用户行为产生的异常。
- 系统异常(SystemException)
- 项目运行过程中可预计但无法避免的异常。
- 比如数据库或服务器宕机。
- 项目运行过程中可预计但无法避免的异常。
- 其他异常(Exception)
- 编程人员未预期到的异常,如:用到的文件不存在。
将异常分类以后,针对不同类型的异常,要提供具体的解决方案:
8.3.2 异常解决方案
| 异常类型 | 解决方案 |
|---|---|
| 业务异常(BusinessException) | 发送对应消息传递给用户,提醒规范操作(如用户名已存在、密码格式不正确等) |
| 系统异常(SystemException) | 发送固定消息安抚用户(系统繁忙,请稍后再试等);发送特定消息给运维人员提醒维护;记录日志 |
| 其他异常(Exception) | 发送固定消息安抚用户;发送特定消息给编程人员提醒维护(纳入预期范围内);记录日志 |
8.3.3 异常解决方案的具体实现
思路:
- 先通过自定义异常,完成 BusinessException 和 SystemException 的定义。
- 将其他异常包装成自定义异常类型。
- 在异常处理器类中对不同的异常进行处理。
步骤1:自定义异常类
//自定义异常处理器,用于封装异常信息,对异常进行分类
public class SystemException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public SystemException(Integer code, String message) {
super(message);
this.code = code;
}
public SystemException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
//自定义异常处理器,用于封装异常信息,对异常进行分类
public class BusinessException extends RuntimeException{
private Integer code;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public BusinessException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}
- 让自定义异常类继承
RuntimeException的好处是,后期在抛出这两个异常的时候,就不用在 try…catch… 或 throws 了。 - 自定义异常类中添加
code属性的原因是为了更好的区分异常是来自哪个业务的。
步骤2:将其他异常包成自定义异常
假如在 BookServiceImpl 的 getById 方法抛异常了,该如何来包装呢?
public Book getById(Integer id) {
//模拟业务异常,包装成自定义异常
if(id == 1){
throw new BusinessException(Code.BUSINESS_ERR,"请不要使用你的技术挑战我的耐性!");
}
//模拟系统异常,将可能出现的异常进行包装,转换成自定义异常
try{
int i = 1/0;
}catch (Exception e){
throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时,请重试!",e);
}
return bookDao.getById(id);
}
具体的包装方式有:
- 方式一:
try{}catch(){}在 catch 中重新 throw 我们自定义异常即可。 - 方式二:直接 throw 自定义异常即可。
上面为了使 code 看着更专业些,我们在 Code 类中再新增需要的属性。
//状态码
public class Code {
public static final Integer SAVE_OK = 20011;
public static final Integer DELETE_OK = 20021;
public static final Integer UPDATE_OK = 20031;
public static final Integer GET_OK = 20041;
public static final Integer SAVE_ERR = 20010;
public static final Integer DELETE_ERR = 20020;
public static final Integer UPDATE_ERR = 20030;
public static final Integer GET_ERR = 20040;
public static final Integer SYSTEM_ERR = 50001;
public static final Integer SYSTEM_TIMEOUT_ERR = 50002;
public static final Integer SYSTEM_UNKNOW_ERR = 59999;
public static final Integer BUSINESS_ERR = 60002;
}
步骤3:处理器类中处理自定义异常
//@RestControllerAdvice用于标识当前类为REST风格对应的异常处理器
@RestControllerAdvice
public class ProjectExceptionAdvice {
//@ExceptionHandler用于设置当前处理器类对应的异常类型
@ExceptionHandler(SystemException.class)
public Result doSystemException(SystemException ex){
//记录日志
//发送消息给运维
//发送邮件给开发人员,ex对象发送给开发人员
return new Result(ex.getCode(),null,ex.getMessage());
}
@ExceptionHandler(BusinessException.class)
public Result doBusinessException(BusinessException ex){
return new Result(ex.getCode(),null,ex.getMessage());
}
//除了自定义的异常处理器,保留对Exception类型的异常处理,用于处理非预期的异常
@ExceptionHandler(Exception.class)
public Result doOtherException(Exception ex){
//记录日志
//发送消息给运维
//发送邮件给开发人员,ex对象发送给开发人员
return new Result(Code.SYSTEM_UNKNOW_ERR,null,"系统繁忙,请稍后再试!");
}
}
步骤4:运行程序
根据 ID 查询:
如果传入的参数为 1,会报 BusinessException。
如果传入的是其他参数,会报 SystemException。
对于异常我们就已经处理完成了,不管后台哪一层抛出异常,都会以我们与前端约定好的方式进行返回,前端只需要把信息获取到,根据返回的正确与否来展示不同的内容即可。
以后项目中的异常处理方式为:所有异常抛到表现层,按业务异常、系统异常、其他异常分类,使用 @RestControllerAdvice + @ExceptionHandler 统一拦截并返回与前端约定格式的 Result。
9. 前后台协议联调
9.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 SSM 整合所需 jar 包。
- 创建对应的配置类。
- 编写 Controller、Service 接口、Service 实现类、Dao 接口和模型类。
- resources 下提供 jdbc.properties 配置文件。
内容参考前面的项目或者直接使用前面的项目进行本节内容的学习。
最终创建好的项目结构如下:
- 将
资料\SSM功能页面下面的静态资源拷贝到 webapp 下。 - 因为添加了静态资源,SpringMVC 会拦截,所有需要在 SpringConfig 的配置类中将静态资源进行放行。
- 新建 SpringMvcSupport。
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
registry.addResourceHandler("/plugins/**").addResourceLocations("/plugins/");
}
}
- 在 SpringMvcConfig 中扫描 SpringMvcSupport。
@Configuration
@ComponentScan({"com.itheima.controller","com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig {
}
接下来我们就需要将所有的列表查询、新增、修改、删除等功能一个个来实现下。
9.2 列表功能
需求:页面加载完后发送异步请求到后台获取列表数据进行展示。
- 找到页面的钩子函数,
created()。created()方法中调用了this.getAll()方法。- 在 getAll() 方法中使用 axios 发送异步请求从后台获取数据。
- 访问的路径为
http://localhost/books。- 返回数据。
返回数据 res.data 的内容如下:
{
"data": [
{
"id": 1,
"type": "计算机理论",
"name": "Spring实战 第五版",
"description": "Spring入门经典教程,深入理解Spring原理技术内幕"
},
{
"id": 2,
"type": "计算机理论",
"name": "Spring 5核心原理与30个类手写实践",
"description": "十年沉淀之作,手写Spring精华思想"
},...
],
"code": 20041,
"msg": ""
}
发送方式:
getAll() {
//发送ajax请求
axios.get("/books").then((res)=>{
this.dataList = res.data.data;
});
}
9.3 添加功能
需求:完成图片的新增功能模块。
- 找到页面上的
新建按钮,按钮上绑定了@click="handleCreate()"方法。- 在 method 中找到
handleCreate方法,方法中打开新增面板。- 新增面板中找到
确定按钮,按钮上绑定了@click="handleAdd()"方法。- 在 method 中找到
handleAdd方法。- 在方法中发送请求和数据,响应成功后将新增面板关闭并重新查询数据。
handleCreate 打开新增面板。
handleCreate() {
this.dialogFormVisible = true;
},
handleAdd 方法发送异步请求并携带数据。
handleAdd () {
//发送ajax请求
//this.formData是表单中的数据,最后是一个json数据
axios.post("/books",this.formData).then((res)=>{
this.dialogFormVisible = false;
this.getAll();
});
}
9.4 添加功能状态处理
基础的新增功能已经完成,但是还有一些问题需要解决下:
需求:新增成功是关闭面板,重新查询数据,那么新增失败以后该如何处理?
- 在 handlerAdd 方法中根据后台返回的数据来进行不同的处理。
- 如果后台返回的是成功,则提示成功信息,并关闭面板。
- 如果后台返回的是失败,则提示错误信息。
(1) 修改前端页面。
handleAdd () {
//发送ajax请求
axios.post("/books",this.formData).then((res)=>{
//如果操作成功,关闭弹层,显示数据
if(res.data.code == 20011){
this.dialogFormVisible = false;
this.$message.success("添加成功");
}else if(res.data.code == 20010){
this.$message.error("添加失败");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
}
(2) 后台返回操作结果,将 Dao 层的增删改方法返回值从 void 改成 int。
public interface BookDao {
// @Insert("insert into tbl_book values(null,#{type},#{name},#{description})")
@Insert("insert into tbl_book (type,name,description) values(#{type},#{name},#{description})")
public int save(Book book);
@Update("update tbl_book set type = #{type}, name = #{name}, description = #{description} where id = #{id}")
public int update(Book book);
@Delete("delete from tbl_book where id = #{id}")
public int delete(Integer id);
@Select("select * from tbl_book where id = #{id}")
public Book getById(Integer id);
@Select("select * from tbl_book")
public List<Book> getAll();
}
(3) 在 BookServiceImpl 中,增删改方法根据 DAO 的返回值来决定返回 true/false。
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
public boolean save(Book book) {
return bookDao.save(book) > 0;
}
public boolean update(Book book) {
return bookDao.update(book) > 0;
}
public boolean delete(Integer id) {
return bookDao.delete(id) > 0;
}
public Book getById(Integer id) {
if(id == 1){
throw new BusinessException(Code.BUSINESS_ERR,"请不要使用你的技术挑战我的耐性!");
}
// //将可能出现的异常进行包装,转换成自定义异常
// try{
// int i = 1/0;
// }catch (Exception e){
// throw new SystemException(Code.SYSTEM_TIMEOUT_ERR,"服务器访问超时,请重试!",e);
// }
return bookDao.getById(id);
}
public List<Book> getAll() {
return bookDao.getAll();
}
}
(4) 测试错误情况,将图书类别长度设置超出范围即可。
处理完新增后,会发现新增还存在一个问题:
新增成功后,再次点击 新增 按钮会发现之前的数据还存在,这个时候就需要在新增的时候将表单内容清空。
resetForm(){
this.formData = {};
}
handleCreate() {
this.dialogFormVisible = true;
this.resetForm();
}
9.5 修改功能
需求:完成图书信息的修改功能。
- 找到页面中的
编辑按钮,该按钮绑定了@click="handleUpdate(scope.row)"。- 在 method 的
handleUpdate方法中发送异步请求根据 ID 查询图书信息。- 根据后台返回的结果,判断是否查询成功。
- 如果查询成功打开修改面板回显数据,如果失败提示错误信息。
- 修改完成后找到修改面板的
确定按钮,该按钮绑定了@click="handleEdit()"。- 在 method 的
handleEdit方法中发送异步请求提交修改数据。- 根据后台返回的结果,判断是否修改成功。
- 如果成功提示错误信息,关闭修改面板,重新查询数据,如果失败提示错误信息。
scope.row 代表的是当前行的行数据,也就是说,scope.row 就是选中行对应的 json 数据,如下:
{
"id": 1,
"type": "计算机理论",
"name": "Spring实战 第五版",
"description": "Spring入门经典教程,深入理解Spring原理技术内幕"
}
修改 handleUpdate 方法。
//弹出编辑窗口
handleUpdate(row) {
// console.log(row); //row.id 查询条件
//查询数据,根据id查询
axios.get("/books/"+row.id).then((res)=>{
if(res.data.code == 20041){
//展示弹层,加载数据
this.formData = res.data.data;
this.dialogFormVisible4Edit = true;
}else{
this.$message.error(res.data.msg);
}
});
}
修改 handleEdit 方法。
handleEdit() {
//发送ajax请求
axios.put("/books",this.formData).then((res)=>{
//如果操作成功,关闭弹层,显示数据
if(res.data.code == 20031){
this.dialogFormVisible4Edit = false;
this.$message.success("修改成功");
}else if(res.data.code == 20030){
this.$message.error("修改失败");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
}
至此修改功能就已经完成。
9.6 删除功能
需求:完成页面的删除功能。
- 找到页面的删除按钮,按钮上绑定了
@click="handleDelete(scope.row)"。- method 的
handleDelete方法弹出提示框。- 用户点击取消,提示操作已经被取消。
- 用户点击确定,发送异步请求并携带需要删除数据的主键 ID。
- 根据后台返回结果做不同的操作。
- 如果返回成功,提示成功信息,并重新查询数据。
- 如果返回失败,提示错误信息,并重新查询数据。
修改 handleDelete 方法。
handleDelete(row) {
//1.弹出提示框
this.$confirm("此操作永久删除当前数据,是否继续?","提示",{
type:'info'
}).then(()=>{
//2.做删除业务
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.code == 20021){
this.$message.success("删除成功");
}else{
this.$message.error("删除失败");
}
}).finally(()=>{
this.getAll();
});
}).catch(()=>{
//3.取消删除
this.$message.info("取消删除操作");
});
}
接下来,下面是一个完整页面。
<!DOCTYPE html>
<html>
<head>
<!-- 页面meta -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>SpringMVC案例</title>
<meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
<!-- 引入样式 -->
<link rel="stylesheet" href="../plugins/elementui/index.css">
<link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
<div class="content-header">
<h1>图书管理</h1>
</div>
<div class="app-container">
<div class="box">
<div class="filter-container">
<el-input placeholder="图书名称" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
<el-button @click="getAll()" class="dalfBut">查询</el-button>
<el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
</div>
<el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
<el-table-column type="index" align="center" label="序号"></el-table-column>
<el-table-column prop="type" label="图书类别" align="center"></el-table-column>
<el-table-column prop="name" label="图书名称" align="center"></el-table-column>
<el-table-column prop="description" label="描述" align="center"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
<el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 新增标签弹层 -->
<div class="add-form">
<el-dialog title="新增图书" :visible.sync="dialogFormVisible">
<el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="图书类别" prop="type">
<el-input v-model="formData.type"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="图书名称" prop="name">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="描述">
<el-input v-model="formData.description" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible = false">取消</el-button>
<el-button type="primary" @click="handleAdd()">确定</el-button>
</div>
</el-dialog>
</div>
<!-- 编辑标签弹层 -->
<div class="add-form">
<el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
<el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
<el-row>
<el-col :span="12">
<el-form-item label="图书类别" prop="type">
<el-input v-model="formData.type"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="图书名称" prop="name">
<el-input v-model="formData.name"/>
</el-form-item>
</el-col>
</el-row>
<el-row>
<el-col :span="24">
<el-form-item label="描述">
<el-input v-model="formData.description" type="textarea"></el-input>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogFormVisible4Edit = false">取消</el-button>
<el-button type="primary" @click="handleEdit()">确定</el-button>
</div>
</el-dialog>
</div>
</div>
</div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
var vue = new Vue({
el: '#app',
data:{
pagination: {},
dataList: [],//当前页要展示的列表数据
formData: {},//表单数据
dialogFormVisible: false,//控制表单是否可见
dialogFormVisible4Edit:false,//编辑表单是否可见
rules: {//校验规则
type: [{ required: true, message: '图书类别为必填项', trigger: 'blur' }],
name: [{ required: true, message: '图书名称为必填项', trigger: 'blur' }]
}
},
//钩子函数,VUE对象初始化完成后自动执行
created() {
this.getAll();
},
methods: {
//列表
getAll() {
//发送ajax请求
axios.get("/books").then((res)=>{
this.dataList = res.data.data;
});
},
//弹出添加窗口
handleCreate() {
this.dialogFormVisible = true;
this.resetForm();
},
//重置表单
resetForm() {
this.formData = {};
},
//添加
handleAdd () {
//发送ajax请求
axios.post("/books",this.formData).then((res)=>{
console.log(res.data);
//如果操作成功,关闭弹层,显示数据
if(res.data.code == 20011){
this.dialogFormVisible = false;
this.$message.success("添加成功");
}else if(res.data.code == 20010){
this.$message.error("添加失败");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
},
//弹出编辑窗口
handleUpdate(row) {
// console.log(row); //row.id 查询条件
//查询数据,根据id查询
axios.get("/books/"+row.id).then((res)=>{
// console.log(res.data.data);
if(res.data.code == 20041){
//展示弹层,加载数据
this.formData = res.data.data;
this.dialogFormVisible4Edit = true;
}else{
this.$message.error(res.data.msg);
}
});
},
//编辑
handleEdit() {
//发送ajax请求
axios.put("/books",this.formData).then((res)=>{
//如果操作成功,关闭弹层,显示数据
if(res.data.code == 20031){
this.dialogFormVisible4Edit = false;
this.$message.success("修改成功");
}else if(res.data.code == 20030){
this.$message.error("修改失败");
}else{
this.$message.error(res.data.msg);
}
}).finally(()=>{
this.getAll();
});
},
// 删除
handleDelete(row) {
//1.弹出提示框
this.$confirm("此操作永久删除当前数据,是否继续?","提示",{
type:'info'
}).then(()=>{
//2.做删除业务
axios.delete("/books/"+row.id).then((res)=>{
if(res.data.code == 20021){
this.$message.success("删除成功");
}else{
this.$message.error("删除失败");
}
}).finally(()=>{
this.getAll();
});
}).catch(()=>{
//3.取消删除
this.$message.info("取消删除操作");
});
}
}
})
</script>
</html>
10. 拦截器
对于拦截器这节的知识,我们需要学习如下内容:
- 拦截器概念
- 入门案例
- 拦截器参数
- 拦截器工作流程分析
10.1 拦截器概念
讲解拦截器的概念之前,我们先看一张图:
(1) 浏览器发送一个请求会先到 Tomcat 的 web 服务器。
(2) Tomcat 服务器接收到请求以后,会去判断请求的是静态资源还是动态资源。
(3) 如果是静态资源,会直接到 Tomcat 的项目部署目录下去直接访问。
(4) 如果是动态资源,就需要交给项目的后台代码进行处理。
(5) 在找到具体的方法之前,我们可以去配置过滤器(可以配置多个),按照顺序进行执行。
(6) 然后进入到到中央处理器(SpringMVC 中的内容),SpringMVC 会根据配置的规则进行拦截。
(7) 如果满足规则,则进行处理,找到其对应的 controller 类中的方法进行执行,完成后返回结果。
(8) 如果不满足规则,则不进行处理。
(9) 这个时候,如果我们需要在每个 Controller 方法执行的前后添加业务,具体该如何来实现?
这个就是拦截器要做的事。
- 拦截器(Interceptor)是一种动态拦截方法调用的机制,在 SpringMVC 中动态拦截控制器方法的执行。
- 作用:
- 在指定的方法调用前后执行预先设定的代码。
- 阻止原始方法的执行。
- 总结:拦截器就是用来做增强。
看完以后,大家会发现:
- 拦截器和过滤器在作用和执行顺序上也很相似。
所以这个时候,就有一个问题需要思考:拦截器和过滤器之间的区别是什么?
- 归属不同:Filter 属于 Servlet 技术,Interceptor 属于 SpringMVC 技术。
- 拦截内容不同:Filter 对所有访问进行增强,Interceptor 仅针对 SpringMVC 的访问进行增强。
10.2 拦截器入门案例
10.2.1 环境准备
- 创建一个 Web 的 Maven 项目。
- pom.xml 添加 SSM 整合所需 jar 包。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>springmvc_12_interceptor</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 创建对应的配置类。
public class ServletContainersInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class[0];
}
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMvcConfig.class};
}
protected String[] getServletMappings() {
return new String[]{"/"};
}
//乱码处理
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter filter = new CharacterEncodingFilter();
filter.setEncoding("UTF-8");
return new Filter[]{filter};
}
}
@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc
public class SpringMvcConfig{
}
- 创建模型类 Book。
public class Book {
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Book{" +
"书名='" + name + '\'' +
", 价格=" + price +
'}';
}
}
- 编写 Controller。
@RestController
@RequestMapping("/books")
public class BookController {
@PostMapping
public String save(@RequestBody Book book){
System.out.println("book save..." + book);
return "{'module':'book save'}";
}
@DeleteMapping("/{id}")
public String delete(@PathVariable Integer id){
System.out.println("book delete..." + id);
return "{'module':'book delete'}";
}
@PutMapping
public String update(@RequestBody Book book){
System.out.println("book update..."+book);
return "{'module':'book update'}";
}
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println("book getById..."+id);
return "{'module':'book getById'}";
}
@GetMapping
public String getAll(){
System.out.println("book getAll...");
return "{'module':'book getAll'}";
}
}
最终创建好的项目结构如下:
10.2.2 拦截器开发
步骤1:创建拦截器类
让类实现 HandlerInterceptor 接口,重写接口中的三个方法。
@Component
//定义拦截器类,实现HandlerInterceptor接口
//注意当前类必须受Spring容器控制
public class ProjectInterceptor implements HandlerInterceptor {
@Override
//原始方法调用前执行的内容
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
return true;
}
@Override
//原始方法调用后执行的内容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
}
@Override
//原始方法调用完成后执行的内容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...");
}
}
拦截器类要被 SpringMVC 容器扫描到。
步骤2:配置拦截器类
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
//注入拦截器
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//配置拦截器 访问/books时拦截
registry.addInterceptor(projectInterceptor).addPathPatterns("/books" );
}
}
步骤3:SpringMVC 添加 SpringMvcSupport 包扫描
@Configuration
@ComponentScan({"com.itheima.controller","com.itheima.config"})
@EnableWebMvc
public class SpringMvcConfig{
}
步骤4:运行程序测试
使用 PostMan 发送 http://localhost/books。
如果发送 http://localhost/books/100 会发现拦截器没有被执行,原因是拦截器的 addPathPatterns 方法配置的拦截路径是 /books,我们现在发送的是 /books/100,所以没有匹配上,因此没有拦截,拦截器就不会执行。
步骤5:修改拦截器拦截规则
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
}
@Override
protected void addInterceptors(InterceptorRegistry registry) {
//配置拦截器
registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*" );
}
}
这个时候,如果再次访问 http://localhost/books/100,拦截器就会被执行。
最后说一件事,就是拦截器中的 preHandler 方法,如果返回 true,则代表放行,会执行原始 Controller 类中要请求的方法,如果返回 false,则代表拦截,后面的就不会再执行了。
步骤6:简化 SpringMvcSupport 的编写
@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc
//实现WebMvcConfigurer接口可以简化开发,但具有一定的侵入性
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置多拦截器
registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
}
}
此后咱们就不用再写 SpringMvcSupport 类了。
最后我们来看下拦截器的执行流程:
当有拦截器后,请求会先进入 preHandle 方法:
- 如果方法返回 true,则放行继续执行后面的 handle[controller 的方法] 和后面的方法。
- 如果返回 false,则直接跳过后面方法的执行。
10.3 拦截器参数
10.3.1 前置处理方法
原始方法之前运行 preHandle。
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
- request:请求对象。
- response:响应对象。
- handler:被调用的处理器对象,本质上是一个方法对象,对反射中的 Method 对象进行了再包装。
使用 request 对象可以获取请求数据中的内容,如获取请求头的 Content-Type。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String contentType = request.getHeader("Content-Type");
System.out.println("preHandle..."+contentType);
return true;
}
使用 handler 参数,可以获取方法的相关信息。
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HandlerMethod hm = (HandlerMethod)handler;
String methodName = hm.getMethod().getName();//可以获取方法的名称
System.out.println("preHandle..."+methodName);
return true;
}
10.3.2 后置处理方法
原始方法运行后运行,如果原始方法被拦截,则不执行。
public void postHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
前三个参数和上面的是一致的。
modelAndView:如果处理器执行完成具有返回结果,可以读取到对应数据与页面信息,并进行调整。
因为咱们现在都是返回 json 数据,所以该参数的使用率不高。
10.3.3 完成处理方法
拦截器最后执行的方法,无论原始方法是否执行。
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception ex) throws Exception {
System.out.println("afterCompletion");
}
前三个参数与上面的是一致的。
ex:如果处理器执行过程中出现异常对象,可以针对异常情况进行单独处理。
因为我们现在已经有全局异常处理器类,所以该参数的使用率也不高。
这三个方法中,最常用的是 preHandle,在这个方法中可以通过返回值来决定是否要进行放行,我们可以把业务逻辑放在该方法中,如果满足业务则返回 true 放行,不满足则返回 false 拦截。
10.4 拦截器链配置
目前,我们在项目中只添加了一个拦截器,如果有多个,该如何配置?配置多个后,执行顺序是什么?
10.4.1 配置多个拦截器
步骤1:创建拦截器类
实现接口,并重写接口中的方法。
@Component
public class ProjectInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...222");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...222");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...222");
}
}
步骤2:配置拦截器类
@Configuration
@ComponentScan({"com.itheima.controller"})
@EnableWebMvc
//实现WebMvcConfigurer接口可以简化开发,但具有一定的侵入性
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
private ProjectInterceptor projectInterceptor;
@Autowired
private ProjectInterceptor2 projectInterceptor2;
@Override
public void addInterceptors(InterceptorRegistry registry) {
//配置多拦截器
registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");
}
}
步骤3:运行程序,观察顺序。
拦截器执行的顺序是和配置顺序有关。就和前面所提到的运维人员进入机房的案例,先进后出。
- 当配置多个拦截器时,形成拦截器链。
- 拦截器链的运行顺序参照拦截器添加顺序为准。
- 当拦截器中出现对原始处理器的拦截,后面的拦截器均终止运行。
- 当拦截器运行中断,仅运行配置在前面的拦截器的 afterCompletion 操作。
| 方法 | 执行顺序 | 是否可能不执行 |
|---|---|---|
| preHandle | 与配置顺序相同,必定运行 | 否 |
| postHandle | 与配置顺序相反 | 是(原始方法被拦截时不运行) |
| afterCompletion | 与配置顺序相反 | 是(运行中断时仅前面拦截器执行) |
一句话总结:SpringMVC 是以 Servlet 为核心的轻量级 Web 框架,通过统一的中央控制器、注解驱动的参数绑定与 @ResponseBody 响应 JSON,并以 RESTful、统一结果封装、全局异常处理与拦截器实现前后端解耦的高效开发。