作用:将html表单提交的post请求转换为put请求或delete请求发给接口。
html不支持put和delete.
一、web.xml中配置过滤器
<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>
二、表单增加一个名为"_method"的隐藏域,值为put或delete
<%--
Created by IntelliJ IDEA.
User: amadeus
Date: 2023/8/27
Time: 16:39
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
Hi world!
<form action="/springmvc2028/hi3" method="post">
<input type="submit" value="submit"/>
<input type="hidden" name="_method" value="put"/>
</form>
</body>
</html>
接口代码:
@PutMapping("/hi3")
public String his(){
return "his";
}
工作原理:调用其doFilterInternal方法,对请求的类型进行判断,如果是_method这个请求参数是put或delete,则用把原有的post请求包装成put或delete请求,然后放行