在基于Spring注释的控制器中,是否可以使用@RequestMapping
将不同的查询字符串映射到不同的方法?
例如
@RequestMapping("/test.html?day=monday")
public void writeMonday() {
}
@RequestMapping("/test.html?day=tuesday")
public void writeTuesday() {
}
答案 0 :(得分:79)
是的,您可以使用params元素:
@RequestMapping("/test.html", params = "day=monday")
public void writeMonday() {
}
@RequestMapping("/test.html", params = "day=tuesday")
public void writeTuesday() {
}
您甚至可以根据参数的状态或缺席进行映射:
@RequestMapping("/test.html", params = "day")
public void writeSomeDay() {
}
@RequestMapping("/test.html", params = "!day")
public void writeNoDay() {
}
答案 1 :(得分:52)
或者您可以执行以下操作:
@RequestMapping("/test.html")
public void writeSomeDay(@RequestParam String day) {
// code to handle "day" comes here...
}