web123456

Spring MVC filter - HiddenHttpMethodFilter

The browser form form only supports GET and POST requests, while DELETE, PUT and other methods do not support it. spring 3.0 has added a filter that can convert these requests into standard http methods, making it support GET, POST, PUT and DELETE requests, which 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 go to the Controller to find the corresponding method. For example, when using annotations, we may use @RequestMapping(value = "list", method = ) in the Controller, so if your form is using <form method="put">, then the form will be submitted to the method marked Method="PUT".

It should be noted that since the doFilterInternal method only filters forms with method post, it must be set as follows on 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, the HiddenHttpMethodFilter must act before the dispatcher, so when configuring the HiddenHttpMethodFilter, you need to refer to the following code:

        <filter>  
                <filter-name>HiddenHttpMethodFilter</filter-name>  
                <filter-class></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></servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:</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 the file. The configurable parameter is methodParam, and the value must beOne of GET, POST, HEAD, OPTIONS, PUT, DELETE, and TRACE.