@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";
}