spring mvc 绑定/验证
所以可以说我有一些模型类:
public class MyRequestParams {
private Long val = Long.valueOf(0); // default value
// ... plus some other stuff
}
我想要的是,如果用户提供 http://host/path?val=& 或 http:// host/path?val=abc,这是无效的参数,我不希望它失败。我希望它只应用默认值。我现在不知道如何执行此操作,除了将 val 存储为字符串并自行进行任何检查以应用默认值。有没有一些简单的方法同时保持 type == Long ?
so lets say I have some model class:
public class MyRequestParams {
private Long val = Long.valueOf(0); // default value
// ... plus some other stuff
}
And what I want, is if the user provides http://host/path?val=& OR http://host/path?val=abc, which is invalid params, I don't want it to fail. I want it to just apply the default. I'm not sure how to do this at the moment except for storing val as a string and doing any checks to apply the default myself. Is there some easy way while keeping the type == Long?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
未经测试,但您可以使用
registerCustomEditor
,在用@InitBinder
。该编辑器将简单地返回默认的长值。
请参阅http://static.springsource。 org/spring/docs/3.0.x/spring-framework-reference/html/validation.html 获取文档和示例。
Not tested, but you could register a custom editor for your property, using
registerCustomEditor
, in a method annotated with@InitBinder
. This editor would simply return the default long value.See http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/validation.html for documentation and examples.