java在使用@RequestMapping注释时获取请求的值(URL)
当我将多个值映射到@RequestMapping(比如Multiple Spring @RequestMapping annotations)时,我能得到请求的值(URL)吗
像这样:
@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET)
public String getCenter(Model model) throws Exception {
String requestedValue = getRequestedValue(); // I want this.
// I want to do something like this with requested value.
String result;
if (requestedValue.equals("center")
result = "center";
else if (requestedValue.equals("left")
result = "left";
return result;
}
# 1 楼答案
从Spring 3.1.0开始,可以使用URI Template Patterns with Regular Expressions
# 2 楼答案
使用RequestParam注释。还可以向方法中添加HttpServletRequest类型的参数,然后从中获取参数
# 3 楼答案
从Spring 3.1.0开始,您可以使用
ServletUriComponentsBuilder
# 4 楼答案
下面的正则表达式将使您的方法仅对URL
/center
和/left
执行。您可以通过@PathVariable
注释获得值