55

In Spring annotation-based controller, is it possible to map different query strings using @RequestMapping to different methods?

For example

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


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

2 Answers 2

80

Yes, you can use the params element:

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

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

You can even map based on the presence or absence of a param:

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

@RequestMapping("/test.html", params = "!day")
public void writeNoDay() {
}
Sign up to request clarification or add additional context in comments.

2 Comments

This showed syntax error for me(on Spring 3), but the following worked. @RequestMapping(value="/test.html", params = "day=monday")
if i have multiple params in url then what to do ??
53

or you could do something like:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.