问题描述
配置<beanid="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><propertyname="basenames"><list><value>com/test/copy/resource</value></list></property></bean>使用Object[]params={"test"};messageSource.getMessage("greeting.common",params,Locale.CHINA);resource文件夹放在WEB-INF/src/com/test/copy/resource下面有resource_en_US.properties和resource_zh_CN.properties两个文件内容均为greeting.common={0}ssa~主要的是<value>com/test/copy/resource</value>怎么配置?按我现在的配置,总是报org.springframework.context.NoSuchMessageException:Nomessagefound.....
解决方案
解决方案二:
在Spring中处理I18N问题和使用Java里面的类基本上是一样的.使用org.springframework.context.support.ResourceBundleMessageSource然后注入资源文件(一个名字为basename的属性),然后就可以在Context中使用资源文件了,如下为一个配置示例:test.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""spring-beans.dtd"><beans><beanid="messageSource"class="org.springframework.context.support.ResourceBundleMessageSource"><propertyname="basename"><!--注意此处设置资源名字和路径--><value>test/i18n/test</value></property></bean></beans>下面为资源文件test.propertiesname=u51B0u96E8sex=u5148u751Ftest_zh.propertiesname=u51B0u96E8sex=u5148u751Ftest_en_US.propertiesname=icerainsex=malepackagetest.i18n;importjava.util.Locale;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassTestI18n{/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubApplicationContextcontext=newClassPathXmlApplicationContext("test/i18n/test.xml");Stringtext=context.getMessage("sex",newObject[0],Locale.US);StringtextZH=context.getMessage("sex",newObject[0],Locale.CHINA);System.out.println(text+"中文:"+textZH);}}很简单,这样就可以了.