1. 在 web.xml 里面配置 HiddenHttpMethodFilter
注意这个 HiddenHttpMethodFilter 应该在 字符编码的Filter 的下面配置
<!--1. 配置HiddenHttpMethodFilter-->
<!--处理GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE请求-->
<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. input 标签添加一个隐藏属性 _method
<input name="_method" value="put" type="hidden"/>
发起图书的增删改查请求;使用Rest风格的URL地址;
请求url 请求方式 表示含义
/book/1 GET: 查询1号图书
/book/1 DELETE: 删除1号图书
/book/1 PUT: 更新1号图书
/book POST: 添加1号图书
从页面发起PUT、DELETE形式的请求?Spring提供了对Rest风格的支持
1)、SpringMVC中有一个Filter;他可以把普通的请求转化为规定形式的请求;配置这个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)、如何发其他形式请求?
按照以下要求;1、创建一个post类型的表单 2、表单项中携带一个_method的参数,3、这个_method的值就是DELETE、PUT
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="book/1">查询图书</a><br/>
<hr>
<form action="book" method="post">
<input type="submit" value="添加1号图书"/>
</form>
<br/>
<hr>
<!-- 发送DELETE请求 -->
<form action="book/1" method="post">
<input name="_method" value="delete" type="hidden"/>
<input type="submit" value="删除1号图书"/>
</form>
<br/>
<hr>
<!-- 发送PUT请求 -->
<form action="book/1" method="post">
<input name="_method" value="put" type="hidden"/>
<input type="submit" value="更新1号图书"/>
</form>
<br/>
<hr>
</body>
</html>
注意:返回的jsp页面需要把 isErrorPage="true" 需要添加上
* 由于是请求转发,而我们转发的是 DELETE,在 jsp页面需要设置 isErrorPage="true" 不然会报错
* HTTP Status 405 – Method Not Allowed
* Type Status Report
* Message JSPs only permit GET POST or HEAD
* Description The method received in the request-line is known by the origin server but not supported by the target resource.
* Apache Tomcat/8.5.43
*
*
* 我这个页面可能会出现异常,异常信息给我封装在 exception 对象里面即可
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true" %>
<%--isErrorPage="true"--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>你成功了,6666</h1>
</body>
</html>
3. Controller 填写对应的请求即可
@Controller
public class BookController {
/**
* 处理查询图书请求
* @param id 图书id
*/
@RequestMapping(value="/book/{bid}",method=RequestMethod.GET)
public String getBook(@PathVariable("bid")Integer id) {
System.out.println("查询到了"+id+"号图书");
return "success";
}
/**
* 图书删除
*
* 由于是请求转发,而我们转发的是 DELETE,在 jsp页面需要设置 isErrorPage="true" 不然会报错
* HTTP Status 405 – Method Not Allowed
* Type Status Report
* Message JSPs only permit GET POST or HEAD
* Description The method received in the request-line is known by the origin server but not supported by the target resource.
* Apache Tomcat/8.5.43
*
*
* 我这个页面可能会出现异常,异常信息给我封装在 exception 对象里面即可
*/
@RequestMapping(value="/book/{bid}",method=RequestMethod.DELETE)
public String deleteBook(@PathVariable("bid")Integer id) {
System.out.println("删除了"+id+"号图书");
return "success";
}
/**
* 图书更新
*/
@RequestMapping(value="/book/{bid}",method=RequestMethod.PUT)
public String updateBook(@PathVariable("bid")Integer id) {
System.out.println("更新了"+id+"号图书");
return "success";
}
@RequestMapping(value="/book",method=RequestMethod.POST)
public String addBook() {
System.out.println("添加了新的图书");
return "success";
}
}