Spring MVC 验证错误代码在哪里解决?
我正在尝试编写 验证器 Spring MVC框架下的,但是文档中有明显的遗漏。调用时将错误传递给 Errors 对象 大多数方法都需要一个名为 errorCode 的字符串参数。如果我理解正确的话,这些错误代码可以作为特定错误消息的替代。但我一生都无法弄清楚这些代码映射到哪里。
这是我从 Spring MVC 的 Javadoc 中引用的示例;
public class UserLoginValidator implements Validator {
private static final int MINIMUM_PASSWORD_LENGTH = 6;
public boolean supports(Class clazz) {
return UserLogin.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "field.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required");
UserLogin login = (UserLogin) target;
if (login.getPassword() != null
&& login.getPassword().trim().length() < MINIMUM_PASSWORD_LENGTH) {
errors.rejectValue("password", "field.min.length",
new Object[]{Integer.valueOf(MINIMUM_PASSWORD_LENGTH)},
"The password must be at least [" + MINIMUM_PASSWORD_LENGTH + "] characters in length.");
}
}
}
谁能启发我吗?
I am attempting to write validators under the Spring MVC framework, but there is a glaring omission in the documentation. When calling passing an error to the Errors object most of the methods expect an String parameter named errorCode. These errorCodes, if I understand correctly serve as stand ins for specific error messages. But I can't for the life figure out where these codes are mapped to.
Here is an example of what I am referring to from Spring MVC's Javadoc;
public class UserLoginValidator implements Validator {
private static final int MINIMUM_PASSWORD_LENGTH = 6;
public boolean supports(Class clazz) {
return UserLogin.class.isAssignableFrom(clazz);
}
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName", "field.required");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "field.required");
UserLogin login = (UserLogin) target;
if (login.getPassword() != null
&& login.getPassword().trim().length() < MINIMUM_PASSWORD_LENGTH) {
errors.rejectValue("password", "field.min.length",
new Object[]{Integer.valueOf(MINIMUM_PASSWORD_LENGTH)},
"The password must be at least [" + MINIMUM_PASSWORD_LENGTH + "] characters in length.");
}
}
}
Can anyone enlighten me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我正在使用默认的消息解析器。
在我的
dispatcher-servlet.xml
中,我其中包含如下映射:
有一个名为“messages.properties”的文本文件, 自定义 MessageCodesResolver 您可以实现 MessageCodeResolver 接口,然后为给定控制器定义解析器,如下所示:
目前还没有一种方法可以全局定义自定义
MessageCodeResolver
; 此处有一个增强请求。 这里描述了一种使用 bean 继承使所有控制器 bean 继承一个控制器 bean 定义的方法。I'm using the default message resolver.
In my
dispatcher-servlet.xml
, I haveand then in the top level of my classes directory I have a text file called "messages.properties" that contains mappings like this:
If you wanted to use a custom MessageCodesResolver you can implement the MessageCodeResolver interface and then define your resolver for a given controller like this:
There isn't currently a way to define a custom
MessageCodeResolver
globally; there's an enhancement request for that here. One approach using bean inheritance to make all controller beans inherit from one controller bean definition, is described here.它们由您的 MessageSource 和 MessagesCodeResolver。
这里是相关部分参考手册:
They are resolved by your MessageSource and a MessagesCodeResolver.
Here is the relevant section in the reference manual:
由于某种原因,如果您想解析控制器中类似于 form:errors jsp 标签的消息,那么您可以执行以下操作:
For some reason if you want to resolve the messages in the Controller similar to form:errors jsp tags then you can do the following: