SpringMVC PathVariable和post、get、put、delete请求

1、PathVariable 可以映射URL中的占位符到目标方法的参数中。

2、Rest风格的URL

  以CRUD为例:

  新增:/order POST

  修改:/order/id PUT

  获取:/order/id GET

  删除:/order/id DELETE

3、如何发送PUT和DELETE请求?

  1.需要配置HiddenHttpMethodFilter

  2.需要发送POST请求: 

  <form action="springmvc/testRest/1" method="post">
  <input type="hidden" name="_method" value="PUT">
  <input type="submit" value="PUT">
  </form>
  <br/><br/>

  <form action="springmvc/testRest/1" method="post">
  <input type="hidden" name="_method" value="DELETE">
  <input type="submit" value="DELETE">
  </form>
  <br/><br/>

  3.需要发送POST请求时携带一个name="_method"的隐藏域,值为DELETE或PUT

   <input type="hidden" name="_method" value="PUT">

   <input type="hidden" name="_method" value="DELETE">

   4.在SpringMVC的目标方法中需要指定请求的方法,并使用@PathVariable注解获取参数值

  @RequestMapping(value="/testRest/{id}",method=RequestMethod.PUT)
  public String testRestPut(@PathVariable Integer id){
  System.out.println("PUT " + id);
  return SUCCESS;
  }

  @RequestMapping(value="/testRest/{id}",method=RequestMethod.DELETE)
  public String testRestDelete(@PathVariable("id") Integer id){
  System.out.println("Delete " + id);
  return SUCCESS;
  }

  @RequestMapping(value="testRest",method=RequestMethod.POST)
  public String testRest(){
  System.out.println("POST");
  return SUCCESS;
  }


  @RequestMapping(value="testRest/{id}",method=RequestMethod.GET)
  public String testRest(@PathVariable("id") Integer id){
  System.out.println("GET " + id);
  return SUCCESS;
  }

  

转载于:https://www.cnblogs.com/Allen-Zsj/p/8072731.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值