【Spring学习笔记-3】国际化支持

【Spring】国际化支持

一、总体结构:

两个国际化资源中的内容:


二、程序

2.1  配置Spring上下文
beans.xml文件

  1. <?xml version="1.0" encoding="GBK"?>
  2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
  6. <bean id="messageSource"
  7. class="org.springframework.context.support.ResourceBundleMessageSource">
  8. <!-- 驱动Spring调用messageSource Bean的setBasenames()方法,
  9. 该方法需要一个数组参数,使用list元素配置多个数组元素 -->
  10. <property name="basenames">
  11. <list>
  12. <value>message1</value>
  13. <!-- 如果有多个资源文件,全部列在此处 -->
  14. </list>
  15. </property>
  16. </bean>
  17. </beans>

说明:


2.2  测试程序

  1. package lee;import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.*;
  3. public class SpringTest
  4. {
  5. public static void main(String[] args)throws Exception
  6. {
  7. // 实例化ApplicationContext
  8. ApplicationContext ctx = new
  9. ClassPathXmlApplicationContext("beans.xml");
  10. // 使用getMessage()方法获取本地化消息。
  11. // Locale的getDefault方法返回计算机环境的默认Locale
  12. String hello = ctx.getMessage("hello" , new String[]{"孙悟空"}
  13. , Locale.getDefault());
  14. String now = ctx.getMessage("now" , new Object[]{new Date()}
  15. , Locale.getDefault());
  16. // 打印出两条本地化消息
  17. System.out.println(hello);
  18. System.out.println(now);
  19. }
  20. }


2.3 运行结果:











原文地址:https://www.cnblogs.com/ssslinppp/p/4378085.html