How to handle multiple inputs with same name in Java?

Question:

How can I handle several input 's with the same name in the backend using Java?

For example:

    <input type='text' name='telefone' ><br>
    <input type='text' name='telefone' ><br>
    <input type='text' name='telefone' ><br>
    <input type='text' name='telefone' >

I tried to receive in the method parameter as an array(String[] telefone) but it didn't work. I tried to also put the [] in the name but it didn't work either.

Ps: I'm using SpringMVC.

Answer:

I will take into account that you are sending this data to the backend using form-data, if so, you can receive a List phones in your controllers, and spring will bind it to you, however your inputs should follow a similar notation that of an array, something like:

    <input type='text' name='telefone[0]' ><br>
    <input type='text' name='telefone[1]' ><br>
    <input type='text' name='telefone[2]' ><br>
    <input type='text' name='telefone[3]' >

On your controller:

    public void salvaTelefones(List<String> telefones) {
      //faz algo.
    }
Scroll to Top
AllEscort