Spring"typemismatch"和必填字段

Law*_*ney 11 java spring-webflow

在Spring Webflow 2.0.x的上下文中......

我处理表单绑定"typemismatches",即尝试将String映射到Integer字段,通过在我的使用中使用以下内容 messages.properties

typeMismatch={0} contains invalid data.
Run Code Online (Sandbox Code Playgroud)

这很好用.

The problem is that if the field that the typeMismatch error occurred on was "required" then I also receive an error for the missing required field, which is logical I guess because the value that was submitted was never bound. ("Required" being defined in a Commons Validation XML file)

So, I dont want to see the "XXX is required field" error message when the field is only missing due to the typeMismatch. How do I resolve this? I thought about overriding initBinder() on the FormAction but quickly got nowhere.....

Yve*_*tin 1

您有多种选择,按优先顺序排列:

  • 有选择地设置消息typeMismatch.target.yourFieldNametypeMismatch.int在资源文件中

  • 实现您自己的验证器,以便您可以在绑定步骤之前整数解析失败时发送专用消息

  • 创建 BindingErrorProcessor 来处理不同类型的解析问题