一、@RequestParamapp
使用@RequestParam 註解獲取GET請求或POST請求提交的參數;ui
Java代碼 url
public String requestparam4(@RequestParam(value="username",required=false) String username)
模板
即經過@RequestParam("username")明確告訴Spring Web MVC使用username進行入參。
test
表示請求中能夠沒有名字爲username 的參數,若是沒有默認爲null,此處須要注意以下幾點:require
原子類型:必須有值,不然拋出異常,若是容許空值請使用包裝類代替。變量
Boolean包裝類型類型:默認Boolean.FALSE,其餘引用類型默認爲null。
List
Java代碼 權限
public String requestparam5( request
@RequestParam(value="username", required=true, defaultValue="zhang") String username)
表示若是請求中沒有名字爲username 的參數,默認值爲「zhang」。
若是請求中有多個同名的應該如何接收呢?如給用戶受權時,可能授予多個權限,首先看下以下代碼:
Java代碼
public String requestparam7(@RequestParam(value="role") String roleList)
若是請求參數相似於url?role=admin&role=user,則實際roleList 參數入參的數據爲「admin,user」,即多個數據之間
使用「,」分割;咱們應該使用以下方式來接收多個請求參數:
Java代碼
public String requestparam7(@RequestParam(value="role") String[] roleList)
public String requestparam8(@RequestParam(value="list") List<String> list)
二、@PathVariable
@PathVariable用於將請求URL中的模板變量映射到功能處理方法的參數上。
Java代碼
@RequestMapping(value="/users/{userId}/topics/{topicId}")
public String test(
@PathVariable(value="userId") int userId,
@PathVariable(value="topicId") int topicId)
如請求的 URL 爲「控制器URL/users/123/topics/456」,則自動將URL 中模板變量{userId}和{topicId}綁定到經過@PathVariable註解的同名參數上,即入參後userId=12三、 topicId=456。代碼在PathVariableTypeController 中。