我已经用同样的方法解决了。 Hibernate 验证器的配置文件位于 META-INF/validation.xml 中
示例验证.xml
<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>com.mycompany.validation.utf8.UTF8ResourceBundleMessageInterpolator</message-interpolator>
</validation-config>
实施UTF 8 ResourceBundle 消息插值器
public class UTF8ResourceBundleMessageInterpolator extends ResourceBundleMessageInterpolator {
public UTF8ResourceBundleMessageInterpolator() {
super(new UTF8ResourceBundleLocator(ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES));
}
}
实施UTF8资源包定位器(PlatformResourceBundleLocator 类的克隆,并进行了小修复)
public class UTF8ResourceBundleLocator implements ResourceBundleLocator {
private static final Logger logger = LoggerFactory.getLogger(UTF8ResourceBundleLocator.class);
protected static final ResourceBundle.Control UTF8_CONTROL = new UTF8Control();
private final String bundleName;
public UTF8ResourceBundleLocator(String bundleName) {
this.bundleName = bundleName;
}
/**
* Search current thread classloader for the resource bundle. If not found,
* search validator (this) classloader.
*
* @param locale The locale of the bundle to load.
* @return the resource bundle or <code>null</code> if none is found.
*/
@Override
public ResourceBundle getResourceBundle(Locale locale) {
ResourceBundle rb = null;
ClassLoader classLoader = GetClassLoader.fromContext();
if (classLoader != null) {
rb = loadBundle(
classLoader, locale, bundleName
+ " not found by thread local classloader"
);
}
if (rb == null) {
classLoader = GetClassLoader.fromClass(PlatformResourceBundleLocator.class);
rb = loadBundle(
classLoader, locale, bundleName
+ " not found by validator classloader"
);
}
return rb;
}
private ResourceBundle loadBundle(ClassLoader classLoader, Locale locale, String message) {
ResourceBundle rb = null;
try {
rb = ResourceBundle.getBundle(
bundleName, locale,
classLoader, UTF8_CONTROL
);
} catch (MissingResourceException ignored) {
logger.trace(message);
}
return rb;
}
private static class GetClassLoader implements PrivilegedAction<ClassLoader> {
private final Class<?> clazz;
private static ClassLoader fromContext() {
final GetClassLoader action = new GetClassLoader(null);
if (System.getSecurityManager() != null) {
return AccessController.doPrivileged(action);
} else {
return action.run();
}
}
private static ClassLoader fromClass(Class<?> clazz) {
if (clazz == null) {
throw new IllegalArgumentException("Class is null");
}
final GetClassLoader action = new GetClassLoader(clazz);
if (System.getSecurityManager() != null) {
return AccessController.doPrivileged(action);
} else {
return action.run();
}
}
private GetClassLoader(Class<?> clazz) {
this.clazz = clazz;
}
@Override
public ClassLoader run() {
if (clazz != null) {
return clazz.getClassLoader();
} else {
return Thread.currentThread().getContextClassLoader();
}
}
}
}
Where UTF8控制class 是来自的班级JSF 2.0 应用程序中具有 UTF-8 编码属性文件的 i18n https://stackoverflow.com/questions/3645491/i18n-with-utf-8-encoded-properties-files-in-jsf-2-0-appliaction