zoukankan      html  css  js  c++  java
  • HiddenHttpMethodFilter

    HiddenHttpMethodFilter:浏览器 form 表单只支持 GET 与 POST 请求,而DELETE、PUT 等 method 并不支 持,Spring3.0 添加了一个过滤器,可以将这些请求转换 为标准的 http 方法,使得支持 GET、POST、PUT 与 DELETE 请求。

    package org.springframework.web.filter;
    
    import java.io.IOException;
    import java.util.Locale;
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletRequestWrapper;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.util.Assert;
    import org.springframework.util.StringUtils;
    
    public class HiddenHttpMethodFilter extends OncePerRequestFilter {
    
        /** Default method parameter: {@code _method} */
        public static final String DEFAULT_METHOD_PARAM = "_method";
    
        private String methodParam = DEFAULT_METHOD_PARAM;
    
    
        /**
         * Set the parameter name to look for HTTP methods.
         * @see #DEFAULT_METHOD_PARAM
         */
        public void setMethodParam(String methodParam) {
            Assert.hasText(methodParam, "'methodParam' must not be empty");
            this.methodParam = methodParam;
        }
    
        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {
    
            String paramValue = request.getParameter(this.methodParam);
            if ("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {
                String method = paramValue.toUpperCase(Locale.ENGLISH);
                HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
                filterChain.doFilter(wrapper, response);
            }
            else {
                filterChain.doFilter(request, response);
            }
        }
    
    
        /**
         * Simple {@link HttpServletRequest} wrapper that returns the supplied method for
         * {@link HttpServletRequest#getMethod()}.
         */
        private static class HttpMethodRequestWrapper extends HttpServletRequestWrapper {
    
            private final String method;
    
            public HttpMethodRequestWrapper(HttpServletRequest request, String method) {
                super(request);
                this.method = method;
            }
    
            @Override
            public String getMethod() {
                return this.method;
            }
        }
    
    }
    View Code
  • 相关阅读:
    SQL CREATE DATABASE 语句
    SQL SELECT INTO 语句
    SQL UNION 和 UNION ALL 操作符
    复盘实战营一期毕业典礼----HHR计划----以太入门课--第一课
    抑郁研究所融资历程分享--以太一堂--直播课
    投资人分享答疑----HHR计划----以太直播课第三课
    重新理解《务实创业》---HHR计划--以太一堂第三课
    HHR计划---作业复盘-直播第三课
    电影推荐算法---HHR计划
    一堂优秀学员吕智钊分享----HHR计划----直播课第二课
  • 原文地址:https://www.cnblogs.com/God-Wang/p/11762358.html
Copyright © 2011-2022 走看看