2

UTF-8でHibernateValidatorのカスタムValidationMessagesを表示する際に問題が発生しました。

一般的なjsfメッセージについては、次のアドバイスに従いました。JSF2.0アプリケーションでUTF-8でエンコードされたプロパティファイルを使用するi18n-クラスTextを作成し、faces-config.xmlで使用しました。これは正しく機能しています。

ただし、このアプローチはValidationMessagesでは機能しません。UTF-8では特殊文字は表示されません。

誰かが私にこれについていくつかのアドバイスを与えることができますか?どうもありがとうございます

4

2 に答える 2

0

https://stackoverflow.com/a/3646601/5072526のように同じ Resource-Bundle を使用する場合、これを行うことができます:

その回答から ResourceBundle を変更して、ロケールを取る追加のコンストラクターを用意します。

public I18NUtf8RessourceBundle(Locale locale) {
    setParent(ResourceBundle.getBundle(BUNDLE_NAME, 
            locale, UTF8_CONTROL));
}

ValidationMessages次に、デフォルト パッケージにクラスを作成します。

public class ValidationMessages extends I18NUtf8RessourceBundle{
   public ValidationMessages() {
        super(null);
    }
}

次に、特定のロケール (_en、_de など) で同じクラスを作成します。

public class ValidationMessages_en extends I18NUtf8RessourceBundle{
    public ValidationMessages_en() {
        super(Locale.ENGLISH);
    }
}

すべての言語で同じことを行い、毎回異なるロケールを渡します。

これで動作します。通常の翻訳と同じ検証メッセージ用のファイルを使用することもできます。

于 2015-10-09T15:13:03.833 に答える
0

私も同じ方法で解決しました。Hibernate バリデーターには、META-INF/validation.xml に構成ファイルがあります。

validation.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>

UTF8ResourceBundleMessageInterpolatorの実装

public class UTF8ResourceBundleMessageInterpolator extends ResourceBundleMessageInterpolator {

    public UTF8ResourceBundleMessageInterpolator() {
        super(new UTF8ResourceBundleLocator(ResourceBundleMessageInterpolator.USER_VALIDATION_MESSAGES));
    }
}

UTF8ResourceBundleLocatorの実装(小さな修正を加えた 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();
            }
        }
    }
}

UTF8Controlクラスは、JSF 2.0 アプリケーションの UTF-8 でエンコードされたプロパティ ファイルを使用した i18nのクラスです。

于 2014-08-07T13:14:19.343 に答える