The form form in html only supports GET and POST requests, but methods such as DELETE and PUT are not supported. Spring3 has added a filter that can The request is converted to a standard http method, allowing GET, POST, PUT and DELETE requests to be supported.
@Bean public FilterRegistrationBean<HiddenHttpMethodFilter> testFilterRegistration3() { FilterRegistrationBean<HiddenHttpMethodFilter> registration = new FilterRegistrationBean<HiddenHttpMethodFilter>(); registration.setFilter(new HiddenHttpMethodFilter());//添加过滤器 registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径 registration.setName("HiddenHttpMethodFilter");//设置优先级 registration.setOrder(2);//设置优先级 return registration; }
Set the method to Post in the form form of the page, and add a hidden field as follows:
<input type="hidden" name="_method" value="put" />
View HiddenHttpMethodFilter source code
String paramValue = request.getParameter(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); } }
As can be seen from the source code, filter Only the Post method is filtered, and a hidden field with the parameter name _method needs to be added. You can also set other parameter names. For example, if you want to set it to _method_, you can set the initialization parameters in the HiddenHttpMethodFilter configuration class: put (methodParam, "_method_" )
It can be seen from HiddenHttpMethodFilter that the method value of the form in html can only be post or get. We can get the put form through HiddenHttpMethodFilter. Parameter key-value pairs, and there is another way to get the parameter key-value pairs of the put form in Spring3, which is to use the HttpPutFormContentFilter filter.
@Bean public FilterRegistrationBean<HttpPutFormContentFilter> testFilterRegistration2() { FilterRegistrationBean<HttpPutFormContentFilter> registration = new FilterRegistrationBean<HttpPutFormContentFilter>(); registration.setFilter(new HttpPutFormContentFilter());//添加过滤器 registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径 registration.setName("HttpPutFormContentFilter");//设置优先级 registration.setOrder(2);//设置优先级 return registration; }
The function of the HttpPutFormContentFilter filter is to obtain the value of the put form and pass it to the method in the Controller marked with the method RequestMethod.put.
Different from HiddenHttpMethodFilter, there is no need to add a hidden field with the parameter name _method in the form, and the method does not have to be post, it can be written directly as put, but this filter can only accept the enctype value as application/x -www-form-urlencoded form, that is to say, when using this filter, the code of the form form must be as follows:
<form action="" method="put" enctype="application/x-www-form-urlencoded"> ...... </form>
In addition, after testing, the json data is also ok,enctype=”application/json”It’s also ok
The above is the detailed content of How to solve the problem that SpringBoot2's PUT request cannot receive parameters. For more information, please follow other related articles on the PHP Chinese website!