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

axk*_*axk 30 java forms spring spring-mvc

有一个选择下拉列表,我想在列表中添加"No selection"项目,在提交时应该给我'null'.我正在使用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;
}
Run Code Online (Sandbox Code Playgroud)

并且jspx部分是

<form:select path="country" items="${countryList}" title="country"/>
Run Code Online (Sandbox Code Playgroud)

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

@Edit:我用自定义验证注释解决了这个问题,该注释检查所选值是否为"No Selection".有更标准,更简单的解决方案吗?

Jac*_*son 39

一种选择:

<form:select path="country" title="country" >
     <form:option value="">&nbsp;</form:option>
     <form:options items="${countryList}" />
</form:select>
Run Code Online (Sandbox Code Playgroud)