java - 从 .properties 文件加载消息时出错

标签 java xml spring spring-mvc properties-file

我试图将我的项目的字符串/消息存储在外部 .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 的消息 beans 文件

<?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通过<import resource="classpath:filename.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 ">

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

最佳答案

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

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

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

关于java - 从 .properties 文件加载消息时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32881250/

相关文章:

java - WHILE 循环中的多个条件

php - 更改根 SimpleXML 元素的文本

javascript - 将xml嵌套到jquery中的无序列表中

xml - 显示带有 Angular 的动态 XML

java - 不同端口上的多个 Spring Boot 实例 - 登录/注销问题

Spring启动非 fatal error ClassNotFoundException

java - ClassNotFoundException : com. sun.jersey.core.util.FeaturesAndProperties 错误

java - 如何解决java.lang.OutOfMemoryError?

java - 有没有等同于 JaxRS 的 Scala

spring - 启动Jetty应用程序,但是 “java.lang.IllegalStateException: Duplicate fragment name”