Spring ResourceBundleMessageSource

问题描述

配置<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);}}很简单,这样就可以了.

时间: 2024-07-30 22:40:55

Spring ResourceBundleMessageSource的相关文章

Spring学习点滴,《Spring in Action》笔记(一)

第二章:装配 Bean -------------------------------------------------------------------------------- 1. <ref>标签中的 bean, local, parent 三个属性的区别(P53) 2. <list><value...</list>和<set><value...</set>可以换着用都 可以为 List, Set 以及数组属性赋值(P55)

史上最强Spring mvc入门

一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <!--configure the setting of springmvcDispatcherServlet and configure the mapping--> <servlet>     <servlet-name>

国际化-spring mvc4 动态切换语言不起作用

问题描述 spring mvc4 动态切换语言不起作用 我利用 SPRING4 MVC配置了动态切换语言,但是在页上点击中文.英文切换的时候不起作用.请高手看看 <?xml version=""1.0"" encoding=""UTF-8""?><beans xmlns=""http://www.springframework.org/schema/beans"" xm

环境-Spring基于请求的的国际化问题

问题描述 Spring基于请求的的国际化问题 spring基于URL请求改变语言环境 已经完成基于浏览器的国际化,现在添加基于URL请求的国际化,查资料后配置如下,资料不全,配置起来后总有问题,发出来步骤,求大虾指点. messageSource 以及 properties已经配置好 1.在xml中添加2.重写类3.后台设置4访问 一个共设置这么多,哪里哪里有陋或者错的,求指点 解决方案 Spring对国际化的支持 Spring使用拦截器支持国际化很方便,使用时只需要两个步骤:一.spring配

Spring框架系列相关面试问题

1.说说你了解的Spring中的设计模式  第一种:简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一.  简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类.  spring中的BeanFactory就是简单工厂模式的体现,根据传入一个唯一的标识来获得bean对象,但是否是在传入参数后创建还是传入参数前创建这个要根据具体情况来定.如下配置,就是在 HelloItxxz 类中创建一个 itxxzBean. 1

Spring bean的配置

Bean的容器 Factory和Context,Context多了运行时上下文,提供读取资源文件,监听等等能力 Bean的生命周期: 实例化->填充属性->BeanNameAware的setBeanName方法, 调用BeanFactoryAware的setBeanFactory()方法,调用ApplicationContextAware的setApplicationContext()方法-->调用BeanPostProcessore的预初始化方法,调用InitializingBean的

自定义 spring mvc 拦截器(近期项目需求实现)

          需求背景:特定文件夹下任何文件不经过登录,全部拦截强制跳转登录,并客户端禁止下载服务器定制文件夹文件           经过1天多时间的各种尝试,自定义式的强大拦截器实现了,废话不说了,直接贴代码啦.    demo:       1>   根目录下 index.html 内容:               <a href="html/index.html">index</a><br/>              <

spring事务的问题,为啥事务不起作用?

问题描述 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource"><ref bean="dataSource"/></property></bean><bea

Spring 表单处理

1. SimpleFormController vs @Controller In XML-based Spring MVC web application, you create a form controller by extending the SimpleFormController class. In annotation-based, you can use @Controller instead. SimpleFormController public class Customer