Reputation: 1356
I have in front end a form where the user selects the year and one or more months from a list of months. These params will be sent to Controller as Get Method. Given an URL like this:
..../{year}/{months}/excel/
Where months would be a variable list of the months selected, i.e [01,02,10
].
How I receive all parameters in the Controller? Here's my controller so far:
@RequestMapping(value = "/{year}/{months}/excel/", method = RequestMethod.GET, produces = EXCEL_FORMAT_HEADER)
public @ResponseBody
ModelAndView getRankingByYearExcel(@PathVariable("year") Integer year,
@RequestParam Map<String, Object> months)
{...}
Upvotes: 3
Views: 2730
Reputation: 1356
I did it like this and worked, declaring months
as array of Strings:
@RequestMapping(value = "/{year}/excel/", method = RequestMethod.GET, produces = EXCEL_FORMAT_HEADER)
public @ResponseBody
ModelAndView getRankingByYearExcel(@PathVariable("year") Integer year,
@RequestParam String[] months)
And in URL sent variable months as array of strings:
../2016/excel/?months=1,3,12
Thanks for guiding me in this direction
Upvotes: 3
Reputation: 4120
Here is how you bind all URI template variable to Map and use.
First of all, You need to change @RequesetParam
to @PathVariable
example1:
@RequestMapping("{id}/messages/{msgId}")
public String handleRequest4 (@PathVariable Map<String, String> varsMap, Model model) {
model.addAttribute("msg", varsMap.toString());
return "my-page";
}
example2:
@GetMapping("/request4/{name}/{age}/address/{city}/{country}")
@ResponseBody
public String handler(@PathVariable Map<String, String> params) {
StringBuilder builder = new StringBuilder();
builder.append("URL parameters - <br>");
for (Entry<String, String> entry : params.entrySet()) {
builder.append(entry.getKey() + " = " + entry.getValue() + "<br>");
}
return builder.toString();
}
For more information see doc1 or see doc2
Upvotes: 2
Reputation: 315
I would change the @RequestParam Map<String, Object> months
to @RequestParam String months
You can then split the months based on comma.
String[] monthsList = months.split(",");
The monthsList
array will have all the user selected values.
Upvotes: 2