有一个选择下拉菜单,我想在列表中添加“无选择”项目,提交时应该给我“空”。 我正在使用 SimpleFormController 派生 Controller 。
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"/>
一种可能的解决方案似乎是在列表的开头添加一个空值,然后使用自定义 PropertyEditor 将此“空”显示为“无选择”。 有没有更好的解决方案?
@Edit:我已经通过自定义验证注释解决了这个问题,该注释检查所选值是否为“无选择”。有没有更标准、更简单的解决方案?
最佳答案
一个选项:
<form:select path="country" title="country" >
<form:option value=""> </form:option>
<form:options items="${countryList}" />
</form:select>
关于java - Spring MVC 表单标签 : Is there a standard way to add "No selection" item?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/162497/