SpringMVC从动态表单输入获取的值具有相同的名称

时间:2014-06-10 21:31:40

标签: spring-mvc multiparameter

这是index.jsp页面:

<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>

这是我的控制器

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

这里的问题是为什么channelLst只有1个元素,它总是表单上的最后一个值,无论用户在表单上添加了多少。

2 个答案:

答案 0 :(得分:1)

我没有遇到任何问题,我使用了你提到的相同代码,我在channelLst中有2个频道enter image description here

我还添加了动态下拉列表,可以正常处理任意数量的下拉列表

答案 1 :(得分:1)

 <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>

现在你将获得值...... vale 属性未被你提供