码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC将post请求转为delete/put请求

时间:2019-11-08 09:32:39      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:post请求   value   orm   form   cal   使用   request   name   tun   

需要在web.xml文件配置
<!--
将POST请求转化为DELETE或者是PUT
要用_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{}

springMVC将post请求转为delete/put请求

标签:post请求   value   orm   form   cal   使用   request   name   tun   

原文地址:https://blog.51cto.com/11585002/2448639

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!