(八)sping之国际化

我今天介绍的国际化呢,平时基本上都用不上,我的项目中也从来没用过,就记得当初学的时候有这么一部分,就回顾比较浅显的一部分了,以备今后使用

首先呢,必须要在Spring配置文件中配置这么一段

Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:tx="http://www.springframework.org/schema/tx"  
  5.     xsi:schemaLocation="  
  6.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  7.             http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
  8.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  9. <!--  
  10.  Spring 通过ApplicationContext 容器的MessageSource 接口实现Sping 对国际化的支持。  
  11. 这里的Bean id 必须是“messageSource”,因为Spring 在装配系统Bean 时会根据这个名字进行查找。  
  12.        这样,我们便可以在程序中通过ApplicationContext 对象调用getMessage()方法获取资源文件的信息。  
  13. -->  
  14.  <bean id="messageSource"  
  15.   class="org.springframework.context.support.ResourceBundleMessageSource"  
  16.   abstract="false" lazy-init="default"  
  17.   autowire="default" dependency-check="default">  
  18.   <property name="basenames">  
  19.   <list>  
  20.    <value>config/properties</value>  
  21.   </list>  
  22.   </property>  
  23.  </bean>  
  24. </beans></span>  

 

两个资源文件

properties_en_US.properties

www.iteye.com=welcome {0},time:{1}

 

properties_zh_CN.properties

www.iteye.com=\u6B22\u8FCE{0},\u65F6\u95F4\:{1}

 

至于\u6B22\u8FCE和\u65F6\u95F4\其实就是‘欢迎’和‘时间’的ASCII码值,在国际化资源文件中,中文汉字必须要配置成ASCII形式的,具体怎么得到这些值的呢

win+r-->运行窗口 输入native2ascii (前提是装了JDK了)可得到如下界面了



 只要输入汉字,然后回车就得到ASCII值

 

好了,来测试一下

Java代码   收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.test;  
  2.   
  3. import java.util.Date;  
  4. import java.util.Locale;  
  5.   
  6. import org.junit.Test;  
  7. import org.springframework.context.ApplicationContext;  
  8. import org.springframework.context.support.FileSystemXmlApplicationContext;  
  9.   
  10. public class CommonTest {  
  11.   
  12.     @Test  
  13.     public void test1() {  
  14.         String path = "WebRoot/WEB-INF/applicationContext.xml";  
  15.         ApplicationContext context = new FileSystemXmlApplicationContext(path);  
  16.         Object[] objs = new Object[] { "javacrazyer",  
  17.                 new Date().toLocaleString() };  
  18.         // www.eimhe.com为资源文件的key值,objs为资源文件value值所需要的参数,Local.CHINA为国际化为什么语言  
  19.         String str = context.getMessage("www.iteye.com", objs, Locale.CHINA);  
  20.         System.out.println(str);  
  21.     }  
  22. }  
  23. </span>  

 

输出结果为

欢迎javacrazyer,时间:2010-9-3 11:00:22

由于代码中指明了语言环境是CHINA,所以会自动去匹配查找zh_CN的资源文件

 

在WEB页面上怎么实现呢,首先添加个sping.tld到WEB-INF/lib下,还需要spring-webmvc.jar

在这之后需在Spring配置文件中添上

Java代码   收藏代码
  1. <span style="font-size: medium;"><bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver"/></span>  

 

看看页面index.jsp

Html代码   收藏代码
  1. <span style="font-size: medium;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib prefix="spring" uri="WEB-INF/lib/spring.tld"%>  
  3. <html>  
  4.   <head>  
  5.     <title>Spring国际化</title>  
  6.   </head>  
  7.   <body>  
  8.      <spring:message code="www.iteye.com" /><br>  
  9.     <input type="button" value="<spring:message code="www.iteye.com" />"/><br>  
  10.   </body>  
  11. </html>  
  12. </span>  

 

由于我们浏览器默认都是中文的,所以显示的结果肯定是中文的啦,怎样显示英文呢,只要更换下浏览器语言即可




 选择好美国英语之后确定,刷新下页面,页面的就变成英文的啦
 

web.xml中还是老样子,最简单的配置

Xml代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  6.     <!-- Spring ApplicationContext Definition -->  
  7.     <context-param>  
  8.         <param-name>contextConfigLocation</param-name>  
  9.         <param-value>/WEB-INF/applicationContext.xml</param-value>  
  10.     </context-param>  
  11.     <listener>  
  12.         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  13.     </listener>  
  14.   
  15.     <welcome-file-list>  
  16.         <welcome-file>index.jsp</welcome-file>  
  17.     </welcome-file-list>  
  18. </web-app>  
  19. </span>  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值