SpringBoot2之PUT请求接收不了参数如何解决
HiddenHttpMethodFilterhtml中form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求。@BeanpublicFilterRegistrationBeantestFilterRegistration3(){FilterRegistrationBeanregistration=newFilterRegistrationBean();r
HiddenHttpMethodFilter

html中form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求。
@Bean public FilterRegistrationBeantestFilterRegistration3() { FilterRegistrationBean registration = new FilterRegistrationBean (); registration.setFilter(new HiddenHttpMethodFilter());//添加过滤器 registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径 registration.setName("HiddenHttpMethodFilter");//设置优先级 registration.setOrder(2);//设置优先级 return registration; }
在页面的form表单中设置method为Post,并添加一个如下的隐藏域:
查看HiddenHttpMethodFilter源码
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);
}
}由源码可以看出,filter只对Post方法进行过滤,且需要添加参数名为_method的隐藏域,也可以设置其他参数名,比如想设置为_method_,可以在HiddenHttpMethodFilter配置类中设置初始化参数:put (methodParam,"_method_")
HttpPutFormContentFilter

由HiddenHttpMethodFilter可知,html中的form的method值只能为post或get,我们可以通过HiddenHttpMethodFilter获取put表单中的参数键值对,而在Spring3中获取put表单的参数键值对还有另一种方法,即使用HttpPutFormContentFilter过滤器。
@Bean public FilterRegistrationBeantestFilterRegistration2() { FilterRegistrationBean registration = new FilterRegistrationBean (); registration.setFilter(new HttpPutFormContentFilter());//添加过滤器 registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径 registration.setName("HttpPutFormContentFilter");//设置优先级 registration.setOrder(2);//设置优先级 return registration; }
HttpPutFormContentFilter过滤器的作为就是获取put表单的值,并将之传递到Controller中标注了method为RequestMethod.put的方法中。
与HiddenHttpMethodFilter不同,在form中不用添加参数名为_method的隐藏域,且method不必是post,直接写成put,但该过滤器只能接受enctype值为application/x-www-form-urlencoded的表单,也就是说,在使用该过滤器时,form表单的代码必须如下:
另外,经过测试,json数据也是ok的,enctype=”application/json”也是ok的
Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















