0
点赞
收藏
分享

微信扫一扫

SpringMVC-配置过滤器HiddenHttpMethodFilter处理PUT和DELETE方法

干自闭 2022-04-15 阅读 92
后端java

一、介绍
(1)正常情况下,如提交表单等请求只支持GET和POST方法。
(2)SpringMVC提供过滤器HiddenHttpMethodFilter,可识别出PUT和DELETE方法。

二、HiddenHttpMethodFilter源码分析(抽取重要部分)

	private String methodParam = "_method";
    
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        HttpServletRequest requestToUse = request;
        //当请求方式是POST方法时进入处理
        if ("POST".equals(request.getMethod()) && request.getAttribute("javax.servlet.error.exception") == null) {
            //methodParam的值为顶部的_method
            //我们可以在表单中添加一个隐藏的input标签并设置其name为_method、value为put或delete
            //然后让后续代码根据_method的值对Request对象进行包装(重新设置其method)
            String paramValue = request.getParameter(this.methodParam);
            if (StringUtils.hasLength(paramValue)) {
                String method = paramValue.toUpperCase(Locale.ENGLISH);
                if (ALLOWED_METHODS.contains(method)) {
                    requestToUse = new HiddenHttpMethodFilter.HttpMethodRequestWrapper(request, method);
                }
            }
        }

        filterChain.doFilter((ServletRequest)requestToUse, response);
    }

三、项目测试
(1)编写index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>index</h1>
<form th:action="@{/info}" method="post">
    <input name="_method" value="put" hidden>
    <div>用户名:<input name="username" type="text"></div>
    <div><input type="submit" value="提交"></div>
</form>
</body>
</html>

(2)编写web.xml文件,配置过滤器HiddenHttpMethodFilter处理PUT和DELETE方法

<?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">
    
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <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>
        <!--设置Response对象返回给浏览器的数据采用的编码字符集为UTF-8-->
        <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>

</web-app>

(3)编写Controller类

@Controller
public class MyController {

    @RequestMapping(value = {"/info"})
    public String info(HttpServletRequest request){
        String method = request.getMethod();
        System.out.println(method);
        return "index";
    }

    @RequestMapping(value = {"/index"})
    public String index(){
        return "index";
    }
}

(4)运行
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

举报

相关推荐

0 条评论