如何设置&#34;未选择&#34;原始值中<form:select>中的值?</form:select>

时间:2014-04-30 06:58:59

标签: forms spring

我正在使用Spring,我会将“未选中”值添加到<form:select>

<form:select path="country.idCountry">
    <form:option value="" label="-- Select one --" />
    <form:options items="${countries}" itemLabel="name" itemValue="idCountry" />
</form:select>

字段idCountry不能为空,因为它是int原语。

如果我尝试执行代码,我会获得此异常:

java.lang.IllegalArgumentException: A null value cannot be assigned to a primitive type

有道理,idCountry字段不能为空,但我想要允许插入“未选中”值(在使用验证器后我检查其值)

我找到了一个丑陋的解决方案,为“未选中”选项设置了一个特殊值,例如-1,然后验证器检查该字段不等于此特殊值。

还有另一种更好的方法吗?

感谢。

0 个答案:

没有答案