Bean 验证 +资源包的想法?
以下是我希望能够在我的 JPA 实体中进行 bean 验证的操作:
- 我想要做的不是
@NotNull
,而是@NotNull(message="keyInMyResourceBundleFile")
- 我还想参数化resourceBundle,但我不知道它的语法,因为在我看来,消息属性只包含一个字符串。
- 参数本身可以进行 i18n 化。例如,假设resourcebundle参数有一个param属性,在英文中,
@NotNull(message="missing.value", params={"credit card"}) StringcreditCard;
就会显示像这样的内容:“信用卡字段缺少必需的值。在印度尼西亚,它会类似于“Nilai Harus di isi untuk Kartu Kredit”。在此示例中,我无法对“信用卡”进行硬编码,因为在印度尼西亚,它是“Kartu Kredit” - 在日志文件或 UI 上显示资源包中定义的错误消息。我不确定如何做到这一点,我应该捕获
ConstraintViolationException
,获取消息,并通过我自己的代码处理资源包吗?
请分享您对此的看法?
谢谢 !
Here is what i want to be able to do with bean validation in my JPA entities :
- Instead of
@NotNull
, i would like to do@NotNull(message="keyInMyResourceBundleFile")
- I would like also to parameterize the resourceBundle, and i dont know the syntax for it, because it seems to me the message attribute contains only a string.
- The parameter itself could be i18n-ed. For example, assuming there's a param attribute for the resourcebundle parameters, in English,
@NotNull(message="missing.value", params={"credit card"}) String creditCard;
It will be displayed something like this : "Missing required value for field credit card. In Indonesia, it'll be something like "Nilai harus di isi untuk Kartu Kredit. In this example, i cant hardcode the "credit card" because in Indonesia, it's "Kartu Kredit" - Displays the error message defined in the resource bundle on the log file or UI. Im not sure the way on how to do it, should i catch the
ConstraintViolationException
, get the message, and process the resource bundle by my own code ?
Please share your thoughts on this ?
Thank you !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于 1 + 2
大括号是参数替换的指示符
关于 3
不知道你在追求什么。 @NotNull 没有 params 属性。我猜你会这样做
@NotNull(message="{missing.credit.card}")
如果你把它放在另一个属性上,你会调用密钥{missing.name}< /em>
关于 4
ConstraintViolationException 包含 *ConstraintViolation* 集合。每个ConstraintViolation 都包含内插消息以及消息模板。如果您想记录它,请这样做...
Regarding 1 + 2
Curly brackets are the indicator of a parameter substitution
Regarding 3
No idea what you are after. There is no params attribute for @NotNull. I guess you would do
@NotNull(message="{missing.credit.card}")
And of if you place it on another property you would call the key {missing.name}
Regarding 4
The ConstraintViolationException contains the set of *ConstraintViolation*s. Each ConstraintViolation contains the interpolated message as well as the message template. If you want to log it, do it ...