Spring MVC - 将空白表单值绑定到原始 Bean 字段
我见过几个类似的问题,但我还没有找到解决这个问题的答案。我有一个 Spring MVC 表单,它由具有原始属性的命令对象支持:
public class MyBean {
private int counter;
public int getCounter() {
return counter;
}
public void setCounter( int counter ) {
this.counter = counter;
}
}
在 JSP 表单中,我有(为了清楚起见而缩写):
<form:form action="count.do">
<form:input id="counter" path="counter"/>
</form:form>
现在,只要“计数器”输入字段中有一个有效的数值,一切都按预期工作,但如果该字段为空,则会出现表单验证错误:
org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'MyBean' on field 'counter': rejected value []; codes [methodInvocation.MyBean.counter,methodInvocation.counter,methodInvocation.float,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [MyBean.counter,counter]; arguments []; default message [counter]]; default message [Property 'counter' threw exception; nested exception is java.lang.IllegalArgumentException]
我尝试创建一个自定义属性编辑器来接受空值(在 initBinder() 中初始化它),但这仅在我创建一个非原始集包装器访问器的数量:
binder.registerCustomEditor( Integer.class, new CustomNumberEditor(Integer.class, true) );
那么,有没有一种方法可以将表单上的字段绑定到原始类型,而不创建非原始包装器,并且仍然处理空白值(我想将它们设置为 0 而不是出错)?如果是这样,您介意发布一个简单的例子吗?
非常感谢, 彼得
I've seen several questions that are similar, but I have yet to find an answer that solves this problem. I have a Spring MVC form that is backed by a command object with a primitive property:
public class MyBean {
private int counter;
public int getCounter() {
return counter;
}
public void setCounter( int counter ) {
this.counter = counter;
}
}
In the JSP form, I have (abbreviated for clarity):
<form:form action="count.do">
<form:input id="counter" path="counter"/>
</form:form>
Now, as long as there is a valid number value in the "counter" input field, everything works as expected, but if the field is blank, then I get a form validation error:
org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'MyBean' on field 'counter': rejected value []; codes [methodInvocation.MyBean.counter,methodInvocation.counter,methodInvocation.float,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [MyBean.counter,counter]; arguments []; default message [counter]]; default message [Property 'counter' threw exception; nested exception is java.lang.IllegalArgumentException]
I've tried creating a custom property editor to accept null values (initializing it in initBinder() ), but this only works if I create a non-primitive set of wrapper accessors:
binder.registerCustomEditor( Integer.class, new CustomNumberEditor(Integer.class, true) );
So, is there a way to bind a field on the form to a primitive type without creating the non-primitive wrappers, and still handle blank values (I'd like to set them to 0 rather than erroring out)? If so, would you mind posting a simple example?
Many thanks,
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从未尝试过,但尝试注册 int.class 的编辑器(这确实有效)。
但是因为
int
无法处理null
new CustomNumberEditor(Integer.class, true)
将不起作用。因此,您需要自己的数字编辑器,它返回例如
0
未定义值。I never tryed it but try to register the Editor for
int.class
(this realy works).But because the an
int
can not handlenull
new CustomNumberEditor(Integer.class, true)
will not work.So you need your own Number Editor that returns for example
0
is not value is defined.