Spring – 基于注释的控制器 – 基于查询string的RequestMapping

在基于Spring注解的控制器中,是否可以使用@RequestMapping将不同的查询string映射到不同的方法?

例如

 @RequestMapping("/test.html?day=monday") public void writeMonday() { } @RequestMapping("/test.html?day=tuesday") public void writeTuesday() { } 

是的,你可以使用params元素:

 @RequestMapping("/test.html", params = "day=monday") public void writeMonday() { } @RequestMapping("/test.html", params = "day=tuesday") public void writeTuesday() { } 

你甚至可以根据是否存在 param来进行映射:

 @RequestMapping("/test.html", params = "day") public void writeSomeDay() { } @RequestMapping("/test.html", params = "!day") public void writeNoDay() { } 

或者你可以做这样的事情:

 @RequestMapping("/test.html") public void writeSomeDay(@RequestParam String day) { // code to handle "day" comes here... }