将验证消息放入数据库 [英] Put validation messages into database
问题描述
我想放置Bean验证(JSR 303)例如使用的验证消息:
I want to put validation messages used by Bean Validation (JSR 303) ex.:
javax.validation.constraints.AssertFalse.message=must be false
javax.validation.constraints.AssertTrue.message=must be true ...
进入数据库,以便管理员添加新语言时,他可以添加该语言的翻译.
into a database so that when an administrator adds a new language he can adds the translations for that language.
我知道如何访问从数据库映射的资源包,但我不知道如何扩展/自定义bean验证类,以便它们可以从数据库访问验证消息...
I know how to access a resource bundle mapped from a database but I can't figure out how to extend/customize the bean validation classes so they can access the validation messages from a database...
能否获得我想要的东西?
Is it possible to obtain what I want ?
在此先感谢您引导我朝正确的方向前进.
Many thanks in advance for driving me into the right direction.
解决方案(我不知道这是否是最好的解决方案,但是它可以工作...):
HERE THE SOLUTION (I don't know if this is the best solution but it works...):
按照@gastaldi的建议,我已经在MessageInterpolator接口上创建了一个实现:
As suggested by @gastaldi I've created an implementation on MessageInterpolator interface:
package giates.validation;
import com.infomaxgroup.adaecommerce.bundles.DatabaseResourceBundle;
import java.util.Locale;
import java.util.Map;
import javax.validation.MessageInterpolator;
public class LocaleMessageInterpolator implements MessageInterpolator {
protected final String BRACE_OPEN = "\\{";
protected final String BRACE_CLOSE = "\\}";
@Override
public String interpolate(String message, Context context) {
return interpolate(message, context, Locale.ITALIAN);
}
@Override
public String interpolate(String message, Context context, Locale locale) {
DatabaseResourceBundle bundle = new DatabaseResourceBundle(locale);
String messageKey = context.getConstraintDescriptor().getAttributes().get("message").toString();
message = bundle.getString(messageKey.replaceAll(BRACE_OPEN, "").replaceAll(BRACE_CLOSE, ""));
Map<String, Object> attributes = context.getConstraintDescriptor().getAttributes();
for (String key : attributes.keySet()) {
String value = attributes.get(key).toString();
key = BRACE_OPEN + key + BRACE_CLOSE;
message = message.replaceAll(key, value);
}
return message;
}
}
然后,我创建了META-INF/validation.xml并添加了自定义的messageinterpolator:
then I've created META-INF/validation.xml and added the customized messageinterpolator:
<?xml version="1.0" encoding="UTF-8"?>
<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration">
<message-interpolator>giates.validation.LocaleMessageInterpolator</message-interpolator>
</validation-config>
我发布验证失败的模型后,插值器就会调用DatabaseResourceBundle并创建结果消息...
as soon as I post a model with failed validations the interpolator calls DatabaseResourceBundle and creates the resulting message...
一切正常!
推荐答案
您需要为此创建一个自定义的MessageInterpolator并在您的validateator.xml中进行配置
You need to create a customized MessageInterpolator for that and configure in your validator.xml
这篇关于将验证消息放入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!