每日一则JavaWeb---SpringMVC的HiddenHttpMethodFilter过滤器

来源:互联网 发布:友盟刷量源码 编辑:程序博客网 时间:2024/05/16 11:16

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

         HiddenHttpMethodFilter的父类是OncePerRequestFilter,它继承了父类的doFilterInternal方法,工作原理是将jsp页面的form表单的method属性值在doFilterInternal方法中转化为标准的Http方法,即GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE,然后到Controller中找到对应的方法。例如,在使用注解时我们可能会在Controller中用于@RequestMapping(value = "list", method = RequestMethod.PUT),所以如果你的表单中使用的是<form method="put">,那么这个表单会被提交到标了Method="PUT"的方法中。

      

HiddenHttpMethodFilter的使用

 需要注意的是,由于doFilterInternal方法只对method为post的表单进行过滤,所以在页面中必须如下设置:

<form action="..." method="post">          <input type="hidden" name="_method" value="put" />          ......  </form>  
    而不是使用:
<form action="..." method="put">          ......  </form>  

     同时,HiddenHttpMethodFilter必须作用于dispatcher前,所以在web.xml中配置HiddenHttpMethodFilter时,需参照如下代码:

 <filter>                  <filter-name>HiddenHttpMethodFilter</filter-name>                  <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>          </filter>          <filter-mapping>                  <filter-name>HiddenHttpMethodFilter</filter-name>                  <servlet-name>spring</servlet-name>          </filter-mapping>        <servlet>  <servlet-name>spring</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  <init-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:dispatcher.xml</param-value>  </init-param>  lt;/servlet>        <servlet-mapping>  <servlet-name>spring</servlet-name>  <url-pattern>*.html</url-pattern>  </servlet-mapping>  
  同样的,作为Filter,可以在web.xml中配置HiddenHttpMethodFilter的参数,可配置的参数为methodParam,值必须为GET,、POST、 HEAD、OPTIONS、PUT、DELETE、TRACE中的一个。

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;}@Overrideprotected 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;}@Overridepublic String getMethod() {return this.method;}}}


原创粉丝点击