Spring4.3.x解决Delete,Put请求参数绑定失败
1. 新增HiddenHttpMethodFilter过滤器处理DELETE,PUT请求
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>springMVC3</servlet-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
SpringMVC请求:
@RequestMapping(value="/test",method=RequestMethod.DELETE)
public String deleteprescription(String content) {
return content;
}
注意baseUrl默认: http://域名:端口/项目名/
前端代码:
$.ajax({
url: ${baseUrl} +'test',
data:{
_method:"DELETE",
content:"helloword"
},
method:'POST',
success:function(res){
console.log(res)
}
2. 直接使用RestFul风格
SpringMVC请求:
@RequestMapping(value="/test/{conetent}",method=RequestMethod.DELETE)
public String deleteprescription(@PathVariable("content") String content) {
return content;
}
前端代码:
$.ajax({
url: ${baseUrl} +'test',
data:{
content:"helloword"
},
method:'DELETE',
success:function(res){
console.log(res)
}