Spring - 基于注释的控制器 - 基于查询字符串的RequestMapping

时间:2009-01-18 05:15:57

标签: spring annotations

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

例如

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


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

2 个答案:

答案 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...
}