Spring MVC Form标签:是否有一种标准方式来添加“未选择”项目? [英] Spring MVC Form tags: Is there a standard way to add "No selection" item?

查看:176
本文介绍了Spring MVC Form标签:是否有一种标准方式来添加“未选择”项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个选择下拉列表,我想在列表中添加无选择项目,当提交时应该给我'空'。
我使用SimpleFormController派生的控制器。
$ b $ pre $ 受保护的映射referenceData(HttpServletRequest httpServletRequest,Object o,Errors errors)throws异常{
Map< String,Object> map = new HashMap< String,Object>();

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

返回地图;
}

而jspx部分是

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

一个可能的解决方案似乎是在列表的开头添加一个空值,然后使用定制PropertyEditor将这个'null'显示为'No selection'。
是否有更好的解决方案?

@Edit:我已经使用自定义验证注释解决了这个问题,该注释检查所选值是否为No Selection。是否有更加标准和更容易的解决方案?

解决方案

一个选项:

< pre class =lang-html prettyprint-override> < form:select path =countrytitle =country>
< form:option value =>& nbsp;< / form:option>
< form:options items =$ {countryList}/>
< / form:select>


There is a select dropdown and I want to add "No selection" item to the list wich should give me 'null' when submitted. I'm using SimpleFormController derived 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;
}

And the jspx part is

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

One possible solution seems to be in adding a null value to the beginning of the list and then using a custom PropertyEditor to disply this 'null' as 'No selection'. Is there a better solution?

@Edit: I have solved this with a custom validation annotation which checks if the selected value is "No Selection". Is there a more standard and easier solution?

解决方案

One option:

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

这篇关于Spring MVC Form标签:是否有一种标准方式来添加“未选择”项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆