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
:
<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:
@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.
You must be logged in to post a comment.