SpringMVC get value from dynamic form input have the same name

1.2k Views Asked by At

This is index.jsp page:

<form method="post">
    <select name="channel">
        <option>Star Movie</option>
        <option>HBO</option>
        ...
        //More television channel option
    </select>
    <select name="channel">
        <option>Star Movie</option>
        <option>HBO</option>
        ...
        //More television channel option
    </select>
    ...
    //More select input added dynamic by user
    <button class="btn-add" >Add channel</button>
    <input type="submit" value="Submit"/>
</form>

This is my Controller

@RequestMapping(value = "/channelReview", method = RequestMethod.POST)
ModelAndView actionChannelReview(@RequestParam("channel") String[] channelLst){
    ...
    //Do something else
}

The problem here is why channelLst is only have 1 element and it always the last value of on the form, no matter how many user add on form.

2

There are 2 best solutions below

0
On
 <form method="post">
        <select name="channel">
            <option value="1">Star Movie</option>
            <option value="2">HBO</option>
            ...
            //More television channel option
        </select>
        <select name="channel">
            <option value="3">Star Movie</option>
            <option value="4">HBO</option>
            ...
            //More television channel option
        </select>
        ...
        //More select input added dynamic by user
        <button class="btn-add" >Add channel</button>
        <input type="submit" value="Submit"/>
    </form>

Now you will got the values ...... vale attribute wasn't given by you

0
On

I did not face any issue, i used the same code that you have mentioned, i got 2 channels in channelLstenter image description here

I also added dynamic dropdowns, working fine with any number of dropdowns