SpringMVC学习的第5天(2021-11-20)
54
前面已经模拟了get和post请求。
get请求:是获取用户信息。(获取全部用户信息或者)
post请求:插入用户信息。
现在模拟put请求来修改用户。
/user PUT
1、index.jsp
2、controller
但是我们发现,它调用的根本不是PUT 而是GET
所以,SPringMVC要处理PUT和DELETE请求的话,需要使用
HiddenHttpMethodFilter
作用:隐藏的http的请求方式。
1、首先在web.xml里面去配置filter
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、表单内容应该还需要加一个隐藏属性
这是之前的(虽然写的是put,但是还是默认走的还是get)
使用HiddenHttpMethodFilter来支持PUT和DELETE
<form th:action="@{/user}" method="post">
<input type="hidden" name="_method" value="put">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="修改用户信息">
</form><br>
学习4:DELETE删除请求
一般删除都是一个超链接。
这个后面再做RESTFul案例的时候来介绍。
SpringMVC web.xml的形式:
一般都是两个过滤器+前端控制器
其中两个过滤器的配置顺序是要求:
CharacterEncodingFilter在前面
HiddenHttpMethodFilter在后面的。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
学习RESTFul的案例:
1、准备bean对象
2、SpringMVC.xml,这个里面需要配置视图解析器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.rtl.mvc.controller" ></context:component-scan>
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"></property>
<property name="characterEncoding" value="UTF-8"></property>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"></property>
<property name="suffix" value=".html"></property>
<property name="templateMode" value="HTML5"></property>
<property name="characterEncoding" value="UTF-8"></property>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
3、写好web.xml,这里需要配置两个过滤器和前端控制器。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
功能清单:
先做好首页:
1、写好首页:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h1>首页</h1>
</body>
</html>
2、写一个访问首页的功能
因为访问首页只需要实现页面跳转,可以使用view-controller标签,直接设置请求地址和视图名称之间的关系。
修改SpringMVC.xml文件
记住,设置了view-controller的时候们为了不影响其他请求的执行,还需要加上注解驱动。
在SpringMVC.xml里面配置,使用mvc这个命名空间就需要加上:
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:annotation-driven></mvc:annotation-driven>
全部的springMVC.xml的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.rtl.mvc" ></context:component-scan>
<bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="1"></property>
<property name="characterEncoding" value="UTF-8"></property>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"></property>
<property name="suffix" value=".html"></property>
<property name="templateMode" value="HTML5"></property>
<property name="characterEncoding" value="UTF-8"></property>
</bean>
</property>
</bean>
</property>
</bean>
<!-- <mvc:view-controller path="/" view-name-->
<mvc:view-controller path="/" view-name="index"></mvc:view-controller>
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
启动,在浏览器上输入:
http://localhost:8080/springMVC/
就可以访问到首页了。
功能1:查询所有的员工信息
首页加上查询超链接。
1、控制层里面写控制方法。
控制器方法可以写的注解有两个
RequestMapping和GetMapping
使用Model对象进行传值。数据给到下面的页面
2、制作显示员工列表信息的页面。
需要从控制器里面设置的model对象拿值:
制作表格来显示员工数据。
employee_list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>员工列表</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0" style="text-align: center;">
<tr>
<th colspan="5">Employee Info</th>
</tr>
<tr>
<th>id</th>
<th>lastName</th>
<th>email</th>
<th>gender</th>
<th>options</th>
</tr>
<tr th:each="employee : ${employeeList}">
<td th:text="${employee.id}"></td>
<td th:text="${employee.lastName}"></td>
<td th:text="${employee.email}"></td>
<td th:text="${employee.gender}"></td>
<td>
<a href="">delete</a>
<a href="">update</a>
</td>
</tr>
</table>
</body>
</html>
学习使用RESTFul的删除功能。
这里的超链接是不能发送delete请求的,
在restful里面,删除还是比较麻烦的。
通过超链接来控制表单的提交,请求参数使用_method
超链接的写法:
//写的这么复杂,就是为了能够成功的把id=1的值,给解析出来
<a th:href="@{'/employee/'+${employee.id}}">delete</a>
这就已经实现了:
将参数值写在路径的后面进行传递。
2、要求当前表单传输为post,且使用HiddenHttpMethodFilter,其中的_method=delete
如何通过超链接来控制表单的提交呢?
通过view来实现,超链接控制表单的提交。
后面的代码需要涉及到vue.js这部分暂时跳过。
https://www.bilibili.com/video/BV1Ry4y1574R?p=60
https://www.bilibili.com/video/BV1Ry4y1574R?p=61
实现添加功能:
点击超链接add,直接跳转到另一个页面:
编辑好信息之后,点击add按钮。
实现方式:
列表页面的options那里写一个超链接add,点击这个add超链接进入到一个表单,用来写你准备添加员工的信息页面。
<th>options(<a th:href="@{/toAdd}">add</a>)</th>
注意:从员工列表/employee页面,点击这个add超链接,跳转到添加页面/add,中间不需要处理其他业务逻辑,也不需要获取其他的数据。
所以,直接在SpringMVC.xml里面配置view-controller
准备好添加页面:
employee_add.html
点击添加按钮的时候,页面就会从当前添加页面再次跳转到列表显示页面。
<form th:action="@{/employee}" method="post">
lastName:<input type="text" name="lastName"><br>
email:<input type="text" name="email"><br>
gender:<input type="radio" name="gender" value="1">male
<input type="radio" name="gender" value="0">female<br>
<input type="submit" name="add"><br>
</form>
HttpMessageConverter:
将请求报文转换为java对象,或者将java对象转换为响应报文。
它提供了两个注解和两个类型:
两个关于请求报文的和两个关于响应报文的。
@RequestBody
@ResponseBody
RequestEntity
ResponseEntity
使用的最多的就是:
@ResponseBody和ResponseEntity
将java对象转换为响应体。
原来向操作响应体的方式:
response.getWriter.write(),将内容响应到浏览器。
有了@ResponseBody和ResponseEntity之后,将Java数据转换为响应体的时候,就非常的简单。
控制器方法上标识注解@ResponseBody,那么这个控制方法的返回值就直接是响应报文的响应体。
学习注解:@ResponseBody(这个注解非常重要)
通过控制器来处理请求的时候,我们现在目前学过的对浏览器的响应方式有几种呢?
1、实现页面跳转,直接响应浏览器一个完整的页面。
有转发和重定向
2、响应浏览器数据
response.getWriter获取PrintWriter对象。
再通过write和print方法响应浏览器。
还没有学习注解Resopnse的时候,在SpringMVC里面响应给浏览器数据的话,使用原生的ServletAPI的HttpServletResponse去响应。
1、
现在使用注解ResponseBody
1、index里面写一个超链接
2、controller
直接返回数据。
以前如果写的是return “success”;且没有@ResponseBody注解的时候,那么就会根据视图解析器,跳转到对应的页面。
加了@ResponseBody响应的是普通的文本类型,
问:可以响应对象给浏览器吗?
比如返回的是一个Employee对象
只要你加了@ResponseBody的注解在控制方法上,那么我们控制方法的返回值是什么,就会给浏览器响应什么数据。之前没有加注解@ResponseBody的时候,返回的字符串都会被Thymeleaf视图解析器解析成一个视图。
1、index做超连接:
2、controller:
注意:我们直接使用@ResponseBody注解,给浏览器响应Employee对象的时候,浏览器是不认识我们的javabean对象的。
目前:响应浏览器的数据只能是一个文本。
怎么解决呢?
将Employee对象转换为json对象
json是一个数据交互格式和xml一样。
当前的SpringMVC是如何处理json的呢?
使用ResponseBody注解来处理json
步骤:
1、导入jackson依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
2、加入依赖之后,直接重新部署项目,都不要做其他操作。
学习:
json:
json有两种格式,一个是对象一个是数组。
如何区分是json对象还是json数组呢?
上面这个是json对象
区分:只要外面是[],就是json数组
只要外面是{},就是json对象
完整的SpringMVC将java对象转换为json对象使用ResponseBody注解传给浏览器。
1、导入jsckson maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
2、在springMVC.xml配置注解驱动
<mvc:annotation-driven></mvc:annotation-driven>
3、将注解@ResponseBody添加到控制方法的方法头上。
4、将java对象直接作为控制器方法的返回值,那么就会自动将javabean对象转换为json格式的字符串。(注意:这里不是直接转换成json对象而是json格式的字符串)
回顾一下json的知识点:
1、json是js里面的一种数据格式
xml也是一种数据交互格式
但是用的多的就是json。
使用ResponseBody注解,可以将控制方法的返回值作为响应给浏览器的响应体。
以后浏览器与服务器之间的交互都使用json
再学完SpringBoot和SpringCloud之后,进入微服务的学习
微服务是以业务逻辑为边界,将一个完整的工程,分成一个个单独运行和单独部署的小工程。
每个小工程里面都是一个微服务。微服务与微服务之间的数据交互用的都是Http+json
以后微服务里面的每一个控制器方法,都需要加上@ResponseBody注解
所以这个@ResponseBody注解非常重要。
复合注解:RestController(这个注解很重要)
这个注解是作用在类上面。
主要是:这个表达:这个类是一个控制器Controller
而且这个类里面的每一个方法都默认加上了@ResponseBody注解。
学习拦截器
发生在DispatcherServlet和Controller之间。
之前学习的过滤器Filter,是在请求和DispatcherServlet之间。
过滤器设置url都是/*
79