3

这是我的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.'

我知道我可以@NotEmpty(message="User Id must not empty")在我的 formBean 中使用,但我想要同步消息,就好像我们需要更改消息一样,它会减少开销。
我搜索了 JSR 文档,但没有找到任何东西来替换我的属性userId名称User Id。请大家帮助我,从两天开始就卡住了。
如果不可能,请告诉我,或者如果不能,请帮助我做些其他的事情。

谢谢沙姆斯
_

4

3 回答 3

4

You've probably fixed this by now, but to set up error messages on JSR 303 formbeans you need to add entries to your property file in the following format:

<Constraint-Name>.<formbean-name>.<attribute-name>=My Message Text

In your case that will be:

NotEmpty.loginFormBean.userId=User Id must not empty

NotEmpty.loginFormBean.password=Password must not empty

where loginFormBean is the name of your form and not the class name.

于 2011-06-16T18:11:49.477 回答
0

作为参考,只需记录BeanPropertyBindingResult实例并检查日志。对于每个违反验证的属性,您会发现四种不同类型的属性代码。

这是一个示例日志供参考

Field error in object 'member' on field 'name': rejected value []; codes [NotEmpty.member.name,NotEmpty.name,NotEmpty.java.lang.String,NotEmpty]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [member.name,name]; arguments []; default message [name]]; default message [may not be empty]

在上面的日志中,您可以找到 codes [NotEmpty.member.name,NotEmpty.name,NotEmpty.java.lang.String,NotEmpty]

当您没有配置任何错误代码或 spring 找不到匹配的代码时,spring 会使用 DefaultMessageCodesResolver 来解决错误消息。检查java doc以查看其他错误代码的格式。

于 2012-01-24T10:30:13.727 回答
0

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

userId=User Id
password=Password

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

祝你好运 :)

于 2011-03-30T19:25:38.717 回答