@RequestParam(value="username", required=true, defaultValue="AAA")
method = {RequestMethod.GET, RequestMethod.POST}
其中不對method做配置則默認支持所有請求方式。
URL通配符映射:
我們還可以通過通配符對URL映射進行配置,通配符有“?”和“*”兩個字符。其中“?”表示1個字符,“*”表示匹配多個字符,“**”表示匹配0個或多個路徑。
例如:
“/helloworld/index?”可以匹配“/helloworld/indexA”、“/helloworld/indexB”,但不能匹配“/helloworld/index”也不能匹配“/helloworld/indexAA”;
“/helloworld/index*”可以匹配“/helloworld/index”、“/helloworld/indexA”、“/helloworld/indexAA”但不能匹配“/helloworld/index/A”;
“/helloworld/index/*”可以匹配“/helloworld/index/”、“/helloworld/index/A”、“/helloworld/index/AA”、“/helloworld/index/AB”但不能匹配 “/helloworld/index”、“/helloworld/index/A/B”;
“/helloworld/index/**”可以匹配“/helloworld/index/”下的多有子路徑,比如:“/helloworld/index/A/B/C/D”;
如果現在有“/helloworld/index”和“/helloworld/*”,如果請求地址爲“/helloworld/index”那麼將如何匹配?Spring MVC會按照最長匹配優先原則(即和映射配置中哪個匹配的最多)來匹配,所以會匹配“/helloworld/index”。
可以使用以下配置來將未正確匹配的路徑引導至一個通用頁面:
@RequestMapping(value="/*")
限制url中必須包含某個參數:
@RequestMapping(value="/paramstest", params="example=AAA", method = {RequestMethod.GET})
其中params允許多個參數:params={"example1", "example2"},表示的是and關係,即兩個參數限制必須同時滿足。限制header同理:
@RequestMapping(value="/headerTest", headers = "example")
@ModelAttribute 我們可以更簡單的講數據添加到Model中
@RequestMapping(value="/modelautobind", method = {RequestMethod.POST})
public String modelAutoBind(HttpServletRequest request, @ModelAttribute("accountmodel") AccountModel accountModel){
return "modelautobindresult";
}