Handling Multiple Parameters with Identical Names in Spring MVC
When a form contains multiple input elements with the same name attribute, Spring MVC processses the submitted data based on the parameter type defined in the controller method. This applies too various input types, including text, checkbox, and hidden fields.
Processing Multiple Text Inputs with Identical Names
Consider a form with two text inputs sharing the same name:
<form action="/submitForm" method="POST">
First Input: <input type="text" name="username"/>
Second Input: <input type="text" name="username"/>
<input type="submit" value="Submit"/>
</form>
In the controller, you can handle this in two ways:
-
Using a String Parameter: Spring MVC concatenates the values with commas.
@PostMapping("/submitForm") public String handleSubmission(@RequestParam("username") String username) { // If inputs are "Alice" and "Bob", username becomes "Alice,Bob" System.out.println(username); return "result"; } -
Using a String Array Parameter: Values are automatically converted into an array.
@PostMapping("/submitForm") public String handleSubmission(@RequestParam("username") String[] usernames) { // usernames array contains ["Alice", "Bob"] System.out.println(Arrays.toString(usernames)); return "result"; }
This behavior mirrors using HttpServletRequest:
String[] values = request.getParameterValues("username");
Handling Checkbox Inputs with Identical Names
For checkboxes, the approach is similar:
<form action="/submitForm" method="POST">
Option 1: <input type="checkbox" name="options" value="opt1"/>
Option 2: <input type="checkbox" name="options" value="opt2"/>
<input type="submit" value="Submit"/>
</form>
Controller methods:
-
Array Parameter: Selected values are stored in an array.
@PostMapping("/submitForm") public String handleCheckboxes(@RequestParam("options") String[] selectedOptions) { // selectedOptions contains values of checked boxes, e.g., ["opt1", "opt2"] System.out.println(Arrays.toString(selectedOptions)); return "result"; } -
String Parameter: Value are concatenated with commas.
@PostMapping("/submitForm") public String handleCheckboxes(@RequestParam("options") String options) { // options becomes "opt1,opt2" if both are selected System.out.println(options); return "result"; }
Processing Hidden Inputs with Identical Names
Hidden inputs follow the same pattern as text inputs:
<form action="/submitForm" method="POST">
<input type="hidden" name="ids" value="1001"/>
<input type="hidden" name="ids" value="1002"/>
<input type="submit" value="Submit"/>
</form>
Controller handling:
@PostMapping("/submitForm")
public String handleHidden(@RequestParam("ids") String[] idList) {
// idList contains ["1001", "1002"]
System.out.println(Arrays.toString(idList));
return "result";
}
Alternatively, use a String parameter for comma-separated values.
In summary, Spring MVC adapts to multiple parameters with the same name by packaging them in to an array or a concatenated string, depending on the controller's parameter type.