Spring中的JSR 303 Bean验证 [英] JSR 303 Bean Validation in Spring

查看:67
本文介绍了Spring中的JSR 303 Bean验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的formbean:-

here is my 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文件中,我写:-

and in my message.properties file i write:-

NotEmpty={0} must not empty.

它向我显示错误消息:userId must not empty.password must not empty.

It shows me error message as: userId must not empty. and password must not empty.

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

But i want to show error message as User Id must not empty. or 'Password must not empty.'

我知道我可以在formBean中使用@NotEmpty(message="User Id must not empty"),但是我希望像需要更改消息一样同步消息,这将减少开销.
我已经搜索了JSR文档,但没有找到任何东西可以随时将我的属性名称userId替换为User Id.请大家帮我,从两天后就陷入困境.
如果不可能,请告诉我,否则,请告诉我.

I know i can use @NotEmpty(message="User Id must not empty") in my formBean but i want synchronize message as if we need to change message, it will be less overhead.
I have searched the JSR docs but not find any thing to replace my attribute name userId to User Id on fly. Please guys help me, stuck in it from two days.
If it is not possible then tell me or if not then help me in something alternative.

谢谢
短发

推荐答案

您可能已经修复了此问题,但是要在JSR 303 Formbeans上设置错误消息,您需要以以下格式将条目添加到属性文件中:

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

在您的情况下,将是:

NotEmpty.loginFormBean.userId=User Id must not empty

NotEmpty.loginFormBean.password=Password must not empty

其中loginFormBean是表单的名称,而不是类名.

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

这篇关于Spring中的JSR 303 Bean验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆