来自外部jar问题的Spring UTF-8消息资源 [英] Spring UTF-8 message resource from external jar issue

查看:89
本文介绍了来自外部jar问题的Spring UTF-8消息资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring MVC应用程序中的UTF-8消息源有问题.我尝试了AbstractMessageSource的两种实现:ResourceBundleMessageSource和ReloadableResourceBundleMessageSource.我有一个外部jar,其中com.mypackage.i18n程序包中包含i18n消息

I have a problem with UTF-8 message sources in Spring MVC application. I've tried two implementations of AbstractMessageSource: ResourceBundleMessageSource and ReloadableResourceBundleMessageSource. I have an external jar with i18n messages contained in com.mypackage.i18n package

ResourceBundleMessageSource的配置:

The configuration for ResourceBundleMessageSource:

<bean id="propertiesMessageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="com.mypackage.i18n.messages" />
    <property name="useCodeAsDefaultMessage" value="true" />
</bean>

此配置加载查找和加载属性,但对于UTF-8失败,因为此实现仅不支持UTF-8.

This configuration loads finds and loads properties, but fails with UTF-8, because this implementation just doesn't support UTF-8.

ReloadableResourceBundleMessageSource的配置:

The configuration for ReloadableResourceBundleMessageSource:

<bean id="propertiesMessageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="com.mypackage.i18n.messages" />
    <property name="useCodeAsDefaultMessage" value="true" />
    <property name="fileEncodings" value="UTF-8" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

此配置找不到属性.我知道这种具有可重载资源的实现需要将属性放在WEB-INF目录中的某个位置,并且如果您不需要重载资源,则它不会将资源限制在其他位置. 根据类java:

This configuration doesn't find properties. I know that this implementation to have reloadable resources needs properties to be located somewhere in WEB-INF directory and it doesn't restrict resources to be located somewhere else if you don't need reloading of resources. According to the class java:

请注意,设置为"basenames"属性的基本名称的方式与ResourceBundleMessageSource的"basenames"属性略有不同.它遵循不指定文件扩展名或语言代码的基本ResourceBundle规则,但可以引用任何Spring资源位置(而不是局限于类路径资源).使用前缀"classpath:",仍然可以从类路径中加载资源,但是在这种情况下,除"-1"(永远缓存)以外的"cacheSeconds"值将不起作用.

Note that the base names set as "basenames" property are treated in a slightly different fashion than the "basenames" property of ResourceBundleMessageSource. It follows the basic ResourceBundle rule of not specifying file extension or language codes, but can refer to any Spring resource location (instead of being restricted to classpath resources). With a "classpath:" prefix, resources can still be loaded from the classpath, but "cacheSeconds" values other than "-1" (caching forever) will not work in this case.

有人可以建议我如何解决该问题:我需要使用另一种方法,或者以某种方式修改ReloadableResourceBundleMessageSource的配置以从jar中查找资源?

Can someone advice me how to solve the problem: I need to use another approach or somehow modify config of ReloadableResourceBundleMessageSource to find resources from jar?

推荐答案

我找到了解决方案.正确的ReloadableResourceBundleMessageSource配置如下所示:

I've found a solution. Proper ReloadableResourceBundleMessageSource configuration looks like this:

<bean id="propertiesMessageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:com/mypackage/i18n/messages" />
    <property name="useCodeAsDefaultMessage" value="false" />
    <property name="fileEncodings" value="UTF-8" />
    <property name="defaultEncoding" value="UTF-8" />
    <property name="cacheSeconds" value="-1"/>
</bean>

这篇关于来自外部jar问题的Spring UTF-8消息资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆