Spring MVC表单标签:是否有添加“无选择”项目的标准方法?


问题内容

有一个选择下拉列表,我想在列表中添加“无选择”项,提交时应给我“空”。我正在使用SimpleFormController派生的控制器。

protected Map referenceData(HttpServletRequest httpServletRequest, Object o, Errors errors) throws Exception {
    Map<String, Object> map = new HashMap<String, Object>();

    map.put("countryList", Arrays.asList(Country.values()));

    return map;
}

而jspx部分是

<form:select path="country" items="${countryList}" title="country"/>

一种可能的解决方案似乎是在列表的开头添加null值,然后使用自定义的PropertyEditor将此“ null”显示为“ No
selection”。有更好的解决方案吗?

@Edit:我已经通过一个自定义验证注释解决了这个问题,该注释检查所选值是否为“ No Selection”。有没有更标准,更简单的解决方案?


问题答案:

一种选择:

<form:select path="country" title="country" >
     <form:option value="">&nbsp;</form:option>
     <form:options items="${countryList}" />
</form:select>