Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从数据库获取的messages.properties_Java_Spring_Jsf_Internationalization - Fatal编程技术网

Java 从数据库获取的messages.properties

Java 从数据库获取的messages.properties,java,spring,jsf,internationalization,Java,Spring,Jsf,Internationalization,可能重复: 我正在研究JSF应用程序的i18n。 我需要从数据库中获取通常位于messages.properties中的所有标准jsf消息。有什么简单的方法吗 谢谢。首先,您需要自己的消息源。查看AbstractMessageSource并对其进行扩展: public class CustomResourceBundleMessageSource extends AbstractMessageSource { @Autowired LocalizationStore loca

可能重复:

我正在研究JSF应用程序的i18n。 我需要从数据库中获取通常位于messages.properties中的所有标准jsf消息。有什么简单的方法吗


谢谢。

首先,您需要自己的消息源。查看AbstractMessageSource并对其进行扩展:

public class CustomResourceBundleMessageSource extends AbstractMessageSource {

    @Autowired
    LocalizationStore localizationStore;

    @Override
    protected MessageFormat resolveCode(String code, Locale locale){
        MessageFormat messageFormat = null;
        ResourceBundle bundle = localizationStore.getBundle(locale);
        try {
            messageFormat = getMessageFormat(bundle, code, locale);
        } catch (MissingResourceException | NullPointerException ex) {
            //Return just the code in case this is used for logging, or throw, your choice
            return createMessageFormat(code, locale);
        }
        return messageFormat;
    }

    protected MessageFormat getMessageFormat(ResourceBundle bundle, String code, Locale locale) throws MissingResourceException {
        String msg = bundle.getString(code);
        return createMessageFormat(msg, locale);
    }
}
您的商店必须返回ResourceBundle:

public class DBResourceBundle extends ResourceBundle {
    @Override
    protected String handleGetObject(String key){
        ...
    }

    @Override
    public Enumeration<String> getKeys() {
        ...
    }
}
这将主要基于您的db模型。我建议在getBundle()方法上使用@cacable,因为您的本地化不太可能经常更改,而且根据您的DB模型,它可能会很昂贵。返回的对象只需为ResourceBundle实现以下方法:

public class DBResourceBundle extends ResourceBundle {
    @Override
    protected String handleGetObject(String key){
        ...
    }

    @Override
    public Enumeration<String> getKeys() {
        ...
    }
}
公共类DBResourceBundle扩展了ResourceBundle{
@凌驾
受保护的字符串句柄对象(字符串键){
...
}
@凌驾
公共枚举getKeys(){
...
}
}
最后,您需要在配置中注册MessageSourcebean:

<bean id="messageSource" class="com.example.CustomResourceBundleMessageSource"/>

我想我找到了答案:

public class DBMessagesBundle extends ResourceBundle {
    @Override
    protected String handleGetObject(String key){
        ...
    }

    @Override
    public Enumeration<String> getKeys() {
        ...
    }
}
公共类DBMessageBundle扩展了ResourceBundle{
@凌驾
受保护的字符串句柄对象(字符串键){
...
}
@凌驾
公共枚举getKeys(){
...
}
}
在FacesConfig.xml中

    <application>
...
        <message-bundle>mypackage.DBMessagesBundle</message-bundle>
    </application>

...
mypackage.DBMessagesBundle

谢谢您的帮助。

是的,我正在考虑扩展ResourceBundle。谢谢。但是我如何告诉jsf使用CustomResourceBundleMessageSource?请输入spring消息标记库