Spring中的JSR 303 Bean验证

时间:2011-03-29 04:21:56

标签: spring-mvc properties bean-validation

这是我的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 must not empty.password must not empty.

但我想将错误消息显示为User Id must not empty.'Password must not empty.'

我知道我可以在我的formBean中使用@NotEmpty(message="User Id must not empty"),但我想要同步消息,就像我们需要更改消息一样,它会减少开销。
我已经搜索了JSR文档,但没有找到任何可以在飞行中将我的属性名称userId替换为User Id的内容。请各位帮助我,两天后坚持下去 如果不可能那么告诉我,如果没有,那么请帮我替代。

感谢
沙姆斯

2 个答案:

答案 0 :(得分:4)

您现在可能已修复此问题,但要在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是表单的名称而不是类名。

答案 1 :(得分:0)

在同一个message.properties文件中,您只需添加以下内容:

userId=User Id
password=Password

或者您可以为字段名称使用单独的文件,为验证消息代码使用单独的文件。这将更具可读性。

祝你好运:)