Spring MVC Filter - HiddenHttpMethodFilter

The browser form only supports GET and POST requests, but DELETE, PUT and other methods are not supported. Spring 3.0 adds a filter that can convert these requests into standard http methods, making it support GET, POST, PUT and DELETE. request, the filter is HiddenHttpMethodFilter.
        The parent class of HiddenHttpMethodFilter is OncePerRequestFilter, which inherits the doFilterInternal method of the parent class. The working principle is to convert the method attribute value of the form form of the jsp page into the standard Http method in the doFilterInternal method, namely GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE, and then find the corresponding method in the Controller. For example, when using annotations we might use @RequestMapping(value = "list", method = RequestMethod.PUT) in the Controller, so if you use <form method="put"> in your form, then this The form will be submitted to the method marked with Method="PUT".
        It should be noted that since the doFilterInternal method only filters the form whose method is post, it must be set as follows in the page:

<form action="..." method="post">  
        <input type="hidden" name="_method" value="put" />  
        ......  
</form>  


instead of using:

<form action="..." method="put">  
        ......  
</form>  


At the same time, HiddenHttpMethodFilter must act before dispatcher, so when configuring HiddenHttpMethodFilter in web.xml, you need to refer to the following code:

<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>  
</servlet>  
      <servlet-mapping>  
<servlet-name>spring</servlet-name>  
<url-pattern>*.html</url-pattern>  
</servlet-mapping>  


  Similarly, as a Filter, you can configure the parameters of HiddenHttpMethodFilter in web.xml. The configurable parameter is methodParam, and the value must be one of GET, POST, HEAD, OPTIONS, PUT, DELETE, and TRACE.

Reprinted from: http://blog.csdn.net/geloin/article/details/7444321

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803466&siteId=291194637