【问题标题】:Error loading messages from .properties file从 .properties 文件加载消息时出错
【发布时间】:2015-12-29 03:13:03
【问题描述】:

我正在尝试将项目的字符串/消息存储在外部 .properties 文件中。我想我已经把所有东西都连接好了,但我还是明白了:

org.springframework.context.NoSuchMessageException: No message found under code 'subtype.user.client' for locale 'null'.

每当我尝试时:

String string = messageSource.getMessage("subtype.user.client", null, null);

我的 spring xml 配置文件如下。由于项目非常大,包含很多 bean,因此我有不同的 spring xml 配置文件定义了不同类型的 bean,还有一个主要的 spring.config.xml 文件将它们连接在一起。

名为messages.subtypes的消息文件

subtype.user.user=User
subtype.user.client=Client props
subtype.user.staff=Staff
subtype.user.clerk=Clerk
subtype.user.secretary=Secretary

名为 spring.messages.config.xml 的消息 bean 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <list>
                <value>messages.subtypes</value>
            </list>
        </property>
    </bean>

    <bean id="myProjectLangs" class="myprojectbase.MyProjectLangs">
        <property name="messageSource" ref="messageSource"></property>
    </bean>

</beans>

主要的spring.config.xml 配置文件,通过&lt;import resource="classpath:filename.xml"/&gt; 将所有bean 连接在一起

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd 
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy />

    <import resource="classpath:spring.messages.config.xml"/>

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource" />

    <context:annotation-config />
    <context:component-scan base-package="myprojectbase"/>

</beans>

【问题讨论】:

  • 你是把消息文件放在classpath还是maven资源文件夹中?
  • 我正在使用 NetBeans。它在

标签: java xml spring spring-mvc properties-file


【解决方案1】:

您收到此错误是因为您将 Locale 参数作为 null 传递。试试

String string = messageSource.getMessage("subtype.user.client", null, Locale.ENGLISH);

即使您没有定义文件messages.subtypes_en.properties,它也应该回退到messages.subtypes.properties

【讨论】:

    【解决方案2】:

    查看您的代码会想到一些可能导致问题的代码:

    • 您的 xml 配置名称包含“.”作为分隔符。这是违反约定的。考虑将配置文件重命名为 spring-messages-config.xml
    • 您的语言属性文件没有属性后缀,再次约定建议将此文件命名为 messages-subtypes.properties
    • 在两个应用程序上下文 xml 文件中,您定义了一个名为 messageSource 的 bean。考虑删除其中一个。
    • 我对为什么您的代码不起作用的主要怀疑在于您在ReloadableResourceBundleMessageSource 上定义basename 的方式。查看setBasename 方法的JavaDoc 有某种形式的配置约定在起作用:

    设置一个基本名称,遵循不指定文件扩展名或语言代码的基本 ResourceBundle 约定,但与 {@link ResourceBundleMessageSource} 指的是 Spring 资源位置相反:例如“WEB-INF/messages.properties”、“WEB-INF/messages_en.properties”等的“WEB-INF/messages”也支持 XML 属性文件:.g. “WEB-INF/messages”也会找到并加载“WEB-INF/messages.xml”、“WEB-INF/messages_en.xml”等。

    这表明,一旦您将消息属性文件重命名为 messages-subtypes.properties,您应该将配置更改为 &lt;value&gt;classpath:messages-subtypes&lt;/value&gt;,确保该文件位于类路径中并且一切都应该开始工作。

    【讨论】:

      【解决方案3】:

      尝试将messages.subtypes 文件重命名为messages.subtypes.properties

      【讨论】:

        猜你喜欢
        • 2011-02-09
        • 2011-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-15
        • 1970-01-01
        相关资源
        最近更新 更多