Spring接收參數的幾種形式

通過Spring controller的機制自動綁定參數

form表單或者通過url傳遞過來的參數,如果參數name和預定義的name一致則可以直接綁定。
Controller 代碼

@RequestMapping("test")
publicvoid test(int count) {
}

或者

@RequestMapping("test")
publicvoid test(Integer count) {
}

Form 代碼

<form action="test" method="post"><input name="count" value="10" type="text"/>
......
</form>

通過註解@RequestParam進行綁定

form表單或者通過url傳遞過來的參數,如果參數name和預定義的name不一致則可以直接綁定。
Controller代碼

@RequestMapping("test")
publicvoid test(@RequestParam("UserName") String username) {
}

Form 代碼

<form action="test" method="post"><input name="UserName" value="用戶名" type="text"/>
......
</form>

通過註解@PathVariable獲取路徑參數

Controller代碼

@RequestMapping(value= "test/{id}/{name} " )  
publicvoid test(@PathVariable String id, @PathVariable String name) {}

請求url :test/12/zhangsan
此時Controller接收到的參數是id=12,name=zhangsan

通過HttpServletRequest獲取參數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章