java - Spring MVC 表单标签 : Is there a standard way to add "No selection" item?

标签 java forms spring spring-mvc

有一个选择下拉菜单,我想在列表中添加“无选择”项目,提交时应该给我“空”。 我正在使用 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="">&nbsp;</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/

相关文章:

javascript - 验证 Google 表单中的电子邮件

spring - 关闭 net.sf.ehcache 和 org.hibernate DEBUG 日志记录

java - 如果我已经在使用@Transactional,我应该什么时候使用entityManager.flush()

java - 如何使用 <s :property/> tag 执行 JavaScript

java - Smack 4.3.2 在 Android 9 上抛出 java.lang.ExceptionInInitializerError 异常

php - 如何以红色显示表单 errorSummary() ?

java - 从 Java 8 升级到 Java 11 后收到加载器约束违规

javascript - 什么时候在 UI 交互中需要机器人保护?

java - Spring ehCache 根据一个bean值跳过缓存

java - 配置类中的 Spring MVC 默认或无参数构造函数