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;
}
