版權聲明:本文爲 小異常 原創文章,非商用自由轉載-保持署名-註明出處,謝謝!
本文網址:https://blog.csdn.net/sun8112133/article/details/107056657
在 Spring MVC 中支持使用 RESTful 風格的 URL 傳參方式,我們來對比一下 傳統 URL 傳參方式 和 RESTful 風格傳參方式:
- 傳統傳參:
http://localhost:8080/hello/index?id=10&name=a
- RESTful 風格傳參:
http://localhost:8080/hello/index/10/a
我們在後臺這樣獲取參數:
@RequestMapping("/rest/{id}/{name}")
public String rest(@PathVariable("id") int id, @PathVariable("name") String name) {
System.out.println("rest...");
System.out.println("id: " + id);
System.out.println("name: " + name);
return "index";
}
注意: @PathVariable
註解是用來獲取路徑參數的,如果參數名相同時,不需要在括號裏指定;若不相同,需要在括號內指定參數。