Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Handling Multiple Parameters with Identical Names in Spring MVC

Tech 2

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:

  1. 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";
    }
    
  2. 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:

  1. 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";
    }
    
  2. 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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.