这是我的formbean:-
public class LoginFormBean {
@NotEmpty
private String userId;
@NotEmpty
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String pass) {
this.password = pass;
}
}
在我的 message.properties 文件中,我写了:-
NotEmpty={0} must not empty.
它向我显示错误消息:userId 不能为空。
和 password 不能为空。
但我想显示错误消息,因为 User Id must not empty.
or 'Password must not
空。'
我知道我可以在我的 formBean 中使用 @NotEmpty(message="User Id must not empty")
但我想要同步消息,就好像我们需要更改消息一样,这样会减少开销。
我已经搜索了 JSR 文档,但没有找到任何可以将我的属性名称 userId
动态替换为 User Id
的东西。请大家帮帮我,两天就卡住了。
如果不可能,请告诉我;如果不可能,请帮我做一些替代的事情。
谢谢
伪装
最佳答案
您现在可能已经解决了这个问题,但是要在 JSR 303 formbeans 上设置错误消息,您需要按照以下格式将条目添加到您的属性文件中:
<Constraint-Name>.<formbean-name>.<attribute-name>=My Message Text
在您的情况下将是:
NotEmpty.loginFormBean.userId=User Id must not empty
NotEmpty.loginFormBean.password=Password must not empty
其中 loginFormBean 是表单的名称而不是类名。
关于spring-mvc - Spring 中的 JSR 303 Bean 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5467861/