zoukankan      html  css  js  c++  java
  • 关于HiddenHttpMethodFilter

    这个类的代码比较少,所以把整个类的代码都复制过来。在注释中添加上自己的理解。

    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 {
    
            HttpServletRequest requestToUse = request;
            //如果这是一个POST请求
            if ("POST".equals(request.getMethod()) && request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) == null) {
                //接着获取参数值比如:PUT、DELETE等
                String paramValue = request.getParameter(this.methodParam);
                //封装成的相应的请求
                if (StringUtils.hasLength(paramValue)) {
                    requestToUse = new HttpMethodRequestWrapper(request, paramValue);
                }
            }
            //最后将请求发送给下一个filter
            filterChain.doFilter(requestToUse, 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.toUpperCase(Locale.ENGLISH);
            }
    
            @Override
            public String getMethod() {
                return this.method;
            }
        }
  • 相关阅读:
    【JVM】程序计数器(四)
    【JVM】运行时数据区概述及线程(三)
    【JVM】类加载子系统(二)
    MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###
    计算机常用指令大全
    HTML的设计与应用
    想得太多,又做的太少
    互联网数据库分库分表现状及一点思考
    python中的函数与文件操作
    python中的流程控制语句
  • 原文地址:https://www.cnblogs.com/GooPolaris/p/7919666.html
Copyright © 2011-2022 走看看