JSP Spring ApplicationContext的國(guó)際化支持
1.ApplicationContext接口繼承了MessageResource接口,因此使用ApplicationContext作為spring容器可以使用國(guó)際化資源文件。
2.在MessageResource接口中定義了兩個(gè)主要用于國(guó)際化的方法:
String getMessage(String param,Object[] args,Locale loc) ;
String getMessage(String param,Object[] args,String default,Locale loc) ;
ApplicationContext就是通過這兩個(gè)方法來完成國(guó)際化的
3.國(guó)際化支持原理:當(dāng)程序創(chuàng)建ApplicationContext時(shí),Spring會(huì)自動(dòng)查找配置文件中名為messageSource的Bean實(shí)例,如果找到這個(gè)實(shí)例,上面兩個(gè)國(guó)際化方法的調(diào)用將會(huì)交給該Bean實(shí)例實(shí)現(xiàn)類;如果沒有找到,將會(huì)沿著父類一直往上找;如果最終還是沒找到,系統(tǒng)將會(huì)創(chuàng)建一個(gè)空的StaticMessageSource Bean,該Bean能接受上述兩個(gè)方法的調(diào)用,但是是空實(shí)現(xiàn)。
4.通常在Spring中使用ResourceBundleMessageSource類來配置messageSource Bean。配置該Bean時(shí)需要提供一個(gè)參數(shù)basenames指定所使用的國(guó)際化資源文件的基本名,該參數(shù)值為list類型,因此需要使用元素來提供參數(shù)值
bean name="messageSource" class="`org.springframework.context.support.ResourceBundleMessageSource">
property name="basenames">
list>
value>message/value>
/list>
/property>
/bean>
!--提供資源文件-->
//message_zh_CN.properties
welcome={0} . 歡迎光臨!現(xiàn)在是北京時(shí)間:{1}
//message_en_US.properties
welcome={0} , welcome! now is:{1}
!--在main程序中的調(diào)用-->
String name = act.getMessage("welcome",new String[]{"成兮”,new Date()},Locale.getDefault(Locale.Category.FORMAT)) ;
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
您可能感興趣的文章:- Spring中BeanFactory和ApplicationContext的作用和區(qū)別(推薦)
- Spring IoC學(xué)習(xí)之ApplicationContext中refresh過程詳解
- SpringBoot獲取ApplicationContext的3種方式
- Spring主配置文件(applicationContext.xml) 導(dǎo)入約束詳解
- Spring通過ApplicationContext主動(dòng)獲取bean的方法講解
- spring中通過ApplicationContext getBean獲取注入對(duì)象的方法實(shí)例
- 詳解Spring Boot的GenericApplicationContext使用教程
- Spring獲取ApplicationContext對(duì)象工具類的實(shí)現(xiàn)方法
- Spring MVC之WebApplicationContext_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- 詳解spring applicationContext.xml 配置文件
- 詳解Spring ApplicationContext加載過程