如何将多个下拉列表绑定到模型 - spring mvc

时间:2012-12-19 13:16:52

标签: java spring-mvc

JSP             

<select name="requirements"  style="width:150px;" id="requirements1">
                                        <option selected="selected" value="o Special Assistance">No Special Assistance</option>
                                        <option value="Assistance climbing steps">Assistance climbing steps</option>
                                        <option value="Assistance within the plane">Assistance within the plane</option>
                                        <option value="Blind passenger">Blind passenger</option>
                                        <option value="Hearing impaired">Hearing impaired</option>
                                        <option value="Wheelchair within terminal">Wheelchair within terminal</option>
                                    </select>
<select name="requirements"  style="width:150px;" id="requirements2">
                                        <option selected="selected" value="o Special Assistance">No Special Assistance</option>
                                        <option value="Assistance climbing steps">Assistance climbing steps</option>
                                        <option value="Assistance within the plane">Assistance within the plane</option>
                                        <option value="Blind passenger">Blind passenger</option>
                                        <option value="Hearing impaired">Hearing impaired</option>
                                        <option value="Wheelchair within terminal">Wheelchair within terminal</option>
                                    </select><input type="text" name="specialrequest" id="specialrequest1" />
        </form>

public class PassengerDetailDTO {

    private String[] requirements;
    private String specialrequest;
}

并在控制器中

@RequestMapping(value = "/manualbooking.htm", method = RequestMethod.POST)
    public String doManualBooking(HttpServletRequest httpServletRequest,
            PassengerDetailDTO passengerDetailDTO,  ModelMap map) {
        LOGGER.info("Manual Booking Request");

        LOGGER.info(passengerDetailDTO.toString());

        return "manualbooking";
    }

如果有多个下拉框,提交表单要求后始终为空。怎么解决?

2 个答案:

答案 0 :(得分:1)

确保您的<select>框名称代表数组[]。例如:

<select name="requirements[0]" ..>

<select name="requirements[1]" ..>

查看此演示:Spring MVC Multi Row Form

答案 1 :(得分:-1)

您可以向辅助bean添加2个不同的字符串字段,即

private String requirement1;
private String requirement2;

然后使用类似于

的构造
 <form:select path="requirement1" multiple="false" id="requirement1">
    <form:options items="${yourRequirementsAsACollection}" />
 </form:select>


 <form:select path="requirement2" multiple="false" id="requirement2">
    <form:options items="${yourRequirementsAsACollection}" />
 </form:select>

将选定的值分别写入第一个或第二个字段。

另一方面,您可以使用多选框,在这种情况下,所有选定的值将以单字段中的逗号分隔结束。

 private String requirements; // Ends up comma-separated, ie "req1, req2"


 <form:select path="requirements" multiple="true" id="requirements">
    <form:options items="${yourRequirementsAsACollection}" />
 </form:select>