首頁  >  文章  >  Java  >  比較比較@RequestParam與@PathVariable的用法差異

比較比較@RequestParam與@PathVariable的用法差異

巴扎黑
巴扎黑原創
2017-07-23 14:30:022236瀏覽

在spring MVC中,兩者的作用都是將request裡的參數的值綁定到contorl裡的方法參數裡的,差別在於,URL寫法不同。

使用@RequestParam時,URL是這樣的:http://host:port/path?參數名稱=參數值

使用@PathVariable時,URL是這樣的: http://host:port/path/參數值

例如:

 

[java] view plain copy
 
  1. #@RequestMapping(value="/user",method = RequestMethod.GET)  

  2.    public @ResponseBody  

  3.    User printUser(@##   User printUser(@ value = "id", required = false, defaultValue = "0")  

  4. int id) {  
  5.     User user = 

    new User();  
  6. ## .getUserById(id);  

  7.        return user;  

  8. ##  }

  9. ##   

    @RequestMapping(value=
  10. "/user/{id:\\d+}",method = RequestMethod.GET)  




##################################如果############   ###public ###@ResponseBody  ##################   User printUser2(###@PathVariable ####   1 int id) {  ##################       User user = ###new User();  #### userService.getUserById(id);  ############       ###return user;  ###############  ############### ###### ######上面兩個方法,存取路徑分別如下:###### ###### ####################

以上是比較比較@RequestParam與@PathVariable的用法差異的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn