springMVC the post request into delete / put requests

Web.xml configuration file need
<! -
POST request into DELETE or PUT
the specified parameters of the request to use the real _method
->
<filter>
<filter-name> HiddenHttpMethodFilter </ filter-name>
<filter- class> org.springframework.web.filter.HiddenHttpMethodFilter </ filter-class>
</ filter>

<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>

</filter-mapping>

在页面上使用隐藏的from提交
<form id="deleteF" action="" method="post">
<input type="hidden" name="_method" value="DELETE"/>

</form>
或者ajax请求提交
var url = "<c:url value="/product/delete"/>"+"/"+pid+".do";
$.ajax({
type : 'post',
data : {'_method':'delete'},(注意:'_method':'delete')
dataType : 'json',
url : url,
success : function(data){
window.location.href="http://localhost:8080/product/findAll.do";
},
error:function(){
window.location.href="http://localhost:8080/product/findAll.do";
}
});

后端使用注解配置
@RequestMapping(value = "/delete/{pid}",method = RequestMethod.DELETE)
public String delete(@PathVariable("pid") String id) throws Exception{}br/>或者:
@DeleteMapping("/delete/{pid}")
public String remove(@PathVariable("pid") int stuNo) throws Exception{}

Guess you like

Origin blog.51cto.com/11585002/2448639