Spring中的JSR 303 Bean验证

编程入门 行业动态 更新时间:2024-10-25 21:16:40
本文介绍了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验证

本文发布于:2023-10-07 17:27:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470022.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSR   Spring   Bean

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!