【问题标题】:ResourceBundle not found for MessageSource未找到 MessageSource 的 ResourceBundle
【发布时间】:2014-08-04 14:46:07
【问题描述】:

我是 spring 的新手,现在我正在尝试使用 aplicationContext 执行 I18N 进程。我正在使用一个 Maven 模块化项目。我的项目包括以下子模块:

model-myApp 服务-myApp webApp-myApp

services-MyApp
   src/main/java     
   src/main/resources

在 services-myApp 资源文件夹中,我用消息源声明了应用程序上下文

<!-- Need to I18N  -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="WEB-INF/lang/myFile"/>
</bean>

在 webApp 中,我有 I18N 所需的文件

webApp-MyApp src/main/webapp/WEB-INF/lang/myFile_it_IT.properties

现在当我尝试获取消息时:

        String message = applicationContext.getMessage("myCode", null,null, Locale.ITALY);

我反而得到了

警告 ResourceBundleMessageSource - 找不到 MessageSource 的 ResourceBundle [/WEB-INF/lang/myFile]:找不到基本名称 /WEB-INF/lang/myFile 的包,语言环境 it_IT

并且消息为空。而如果我在 webApp 类路径 (/src/main/resources) 中移动文件并将 messageSource 的值更改为 myFile 一切正常。

我该如何解决?

【问题讨论】:

    标签: java spring maven maven-3


    【解决方案1】:

    尝试使用ReloadableResourceBundleMessageSource 而不是 ResourceBundleMessageSource。它将在 WEB-INF 下查找文件。

    【讨论】:

      【解决方案2】:

      目前,我正在学习 Spring Boot,本地化配置花了我一段时间才开始工作。

      特别是为属性文件找到正确的位置是一个问题。将它们放在src/main/resources/ 中很好,因为很容易设置对该位置的引用。 (见要点)

      知道ResourceBundleMessageSource 中的基本名称自动相对于资源路径也很重要。

      如果您喜欢使用基于配置注释而不是 XML。请查看我的 Gist 以获取示例配置。

      https://gist.github.com/AWolf81/d3ca839e711810345e42

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-15
        • 2012-11-24
        • 2015-02-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        相关资源
        最近更新 更多