问题描述
我正在尝试使用 MessageSource
检索默认验证错误消息。我正在使用的代码使用反射来检索消息
参数的值。在不覆盖消息
参数的约束上,我想检索默认错误消息。当我在验证注释上调用消息
方法时,我得到 {org.hibernate.validator.constraints.NotBlank.message}
(例如,对于 @NotBlank
注释)。然后我尝试使用 MessageSource
来获取如下错误消息:
I'm trying to retrieve a default validation error-message using MessageSource
. The code I'm working with uses reflection to retrieve the value of the message
parameter. On a constraint that does not override the message
parameter, I would like to retrieve the default error message. When I invoke the message
method on the validation annotation, I get {org.hibernate.validator.constraints.NotBlank.message}
(for example, for the @NotBlank
annotation). I then tried to use MessageSource
to get the error message like so:
String message = messageSource.getMessage(key, null, Locale.US);
我尝试将键
设置为 {org.hibernate.validator.constraints.NotBlank.message}
, org.hibernate.validator.constraints.NotBlank.message
(已删除大括号)甚至 org.hibernate.validator.constraints.NotBlank
但我一直得到 null
。我在这里做错了什么?
I tried setting key
to {org.hibernate.validator.constraints.NotBlank.message}
, org.hibernate.validator.constraints.NotBlank.message
(removed braces) and even org.hibernate.validator.constraints.NotBlank
but I keep getting null
. What am I doing wrong here?
更新
澄清。我的印象是Spring为其约束提供了一个默认的 message.properties
文件。我在这个假设中是否正确?
A clarification. I am under the impression that Spring comes with a default message.properties
file for its constraints. Am I correct in this assumption?
更新
更改名称问题是为了更好地反映我想要做的事情。
Changing the name of the question to better reflect what I was trying to do.
推荐答案
在遇到以及在Hibernate中挖掘之后验证器来源,我想我已经弄明白了:
After running into a blog post from one of the Hibernate guys, and after digging around in the Hibernate Validator source, I think I've figured it out:
public String getMessage(Locale locale, String key) {
String message = key;
key = key.toString().replace("{", "").replace("}", "");
PlatformResourceBundleLocator bundleLocator = new PlatformResourceBundleLocator(ResourceBundleMessageInterpolator.DEFAULT_VALIDATION_MESSAGES);
ResourceBundle resourceBundle = bundleLocator.getResourceBundle(locale);
try {
message = ResourceBundle.getString(key);
}
catch(MissingResourceException ) {
message = key;
}
return message;
}
首先,你必须实例化 PlatformResourceBundleLocator
使用默认验证消息。然后从定位器中检索 ResourceBundle
并使用它来获取您的消息。我不相信这会执行任何插值。为此你必须使用插值器;我上面链接的博客文章详细介绍了这一点。
So first, you have to instantiate a PlatformResourceBundleLocator
with the default validation messages. Then you retrieve a ResourceBundle
from the locator and use that to get your message. I don't believe this performs any interpolation though. For that you have to use an interpolator; the blog post I linked to above goes into more detail about that.
UPDATE
另一种(更简单)方法是更新 applicationContext.xml
并执行此操作:
Another (easier) way is to update your applicationContext.xml
and do this:
<bean id="resourceBundleSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>org.hibernate.validator.ValidationMessages</value>
</list>
</property>
</bean>
现在你的 MessageSource
填充了默认值消息,你可以做 messageSource.getMessage()
。事实上,这可能是最好的方式。
Now your MessageSource
is populated with the default messages and you can do messageSource.getMessage()
. In fact, this is probably the best way.
这篇关于如何从Hibernate Validator中检索默认验证消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!