Here’s another handy stuff I found on Spring MVC, if you have an unknown number of elements on your form (say a fruit basket), you can bind them into a List with @RequestParam
annotation.
Each text input has the same name fruits
:
1
2
3
4
5
6 |
< form method = "post" > Fruit 1: < input type = "text" name = "fruits" />< br /> Fruit 2: < input type = "text" name = "fruits" />< br /> Fruit 3: < input type = "text" name = "fruits" />< br /> < input type = "submit" /> </ form > |
On your controller’s handler method, you can obtain the list of all fruit names by binding it like this:
1
2
3
4 |
@RequestMapping (value = "/" , method = RequestMethod.POST) public String addFruits( @RequestParam ( "fruits" ) List<String> fruits) { // ... } |
The order of fruit names added to the list will be the same as the order of your form text inputs.
참조 : http://gerrydevstory.com/2013/11/14/binding-a-list-request-parameter-on-spring-mvc/