【发布时间】: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 配置文件,通过<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>
【问题讨论】:
-
你是把消息文件放在classpath还是maven资源文件夹中?
-
我正在使用 NetBeans。它在
中
标签: java xml spring spring-mvc properties-file