restful风格中的put请求和delete请求的用法

Restful风格中put请求和delete请求

Restful风格中的CRUD:

增加:/result POST、修改:/result/1 PUT、获取:/result/1 GET、删除:/result/1 DELETE

范例:

web.xml中配置org.springframework.web.filter.HiddenHttpMethodFilter类,可以把post请求转为delete或put。

  1. <filter>  
  2.   <filter-name>HiddenHttpMethodFilter</filter-name>  
  3.   <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.   <filter-name>HiddenHttpMethodFilter</filter-name>  
  7.   <url-pattern>/*</url-pattern>  
  8. </filter-mapping>  
控制器:

  1.        @RequestMapping(value="/testRest/{id}",method=RequestMethod.GET)  
  2. public String testRest(@PathVariable("id") Integer id){  
  3.     System.out.println("testRestGet:"+id);  
  4.     return "success";  
  5. }  
  6.   
  7. @RequestMapping(value="/testRest",method=RequestMethod.POST)  
  8. public String testRest(){  
  9.     System.out.println("testRestPost");  
  10.     return "success";  
  11. }  
  12.   
  13. @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)  
  14. public String testRestDelete(@PathVariable("id") Integer id){  
  15.     System.out.println("testRestDelete:"+id);  
  16.     return "success";  
  17. }  
  18.   
  19. @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)  
  20. public String testRestPut(@PathVariable("id") Integer id){  
  21.     System.out.println("testRestPut:"+id);  
  22.     return "success";  
  23. }  
请求页面:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  5. <title>index</title>  
  6. </head>  
  7. <body>  
  8.     <a href="SpringAnnotation/testRest/1">TestRestGet</a>  
  9.     <br/><br/>  
  10.     <form action="SpringAnnotation/testRest" method="post">  
  11.         <input type="submit" value="TestRestPost"/>  
  12.     </form>  
  13.     <br/><br/>  
  14.     <form action="SpringAnnotation/testRest/1" method="post">  
  15.         <input type="hidden" name="_method" value="DELETE"/>  
  16.         <input type="submit" value="TestRestDelete"/>  
  17.     </form>  
  18.     <br/><br/>  
  19.     <form action="SpringAnnotation/testRest/1" method="post">  
  20.         <input type="hidden" name="_method" value="PUT"/>  
  21.         <input type="submit" value="TestRestPut"/>  
  22.     </form>  
  23. </body>  
  24. </html>  

如何发送DELETE和PUT请求?

(1). 需要配置 HiddenHttpMethodFilter (2). 需要发送 POST 请求(3). 需要在发送 POST 请求时携带一个 name="_method" 的隐藏域, 值为 DELETE 或 PUT

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值