(九)struts2之国际化

1. Java对国际化的支持: Java内部使用unicode编码方式。
  1) java.util.Locale 表示了特定的地理、政治和文化地区. 环境信息对象。
            由语言代码和区域代码组成。
             语言代码小写字母: en,zh
             区域代码大写字母: US,CN

  2) java.util.ResourceBundle 用于绑定特定语言环境的资源对象。
             资源文件的命名规范:基本名_语言代码[_区域代码].properties
             默认资源文件名:基本名.properties
            中国:基本名_zh_CN.properties
            美国:基本名_en_US.properties
    
  3) java.text.MessageFormat 用于对含有占位符的字符进行格式化输出。
  4) 编码方式:
     Locale locale = Locale.CHINA;
     ResourceBundle rb  = ResourceBundle.getBundle("资源文件的基本名", locale);
     String value = rb.getString("key");
     String str = MessageFormat.format(value, Object... arguments);

2. Struts2的国际化应用
  1) 在classpath中添加针对特定语言环境的资源文件
  2) 在struts.xml中通过常量配置注册资源文件的基本名:
     <constant name="struts.custom.i18n.resources" value="msg"/>
  3) 在JSP页面中,用<s:text value="资源文件中的key"/>就可以取出本地化的消息
     Struts2的表单标签都有一个属性key用来取出本地化的消息
  4) 在Action中,可以使用ActionSupport类提供的getText("key", 给占位符传值的对象数组);也可以获取本地化的消息

 

具体示例

index.jsp

Java代码   收藏代码
  1. <span style="font-size: medium;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.     <title><s:text name="index_title"/></title>  
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.     <!--  
  13.     <link rel="stylesheet" type="text/css" href="styles.css">  
  14.     -->  
  15.   </head>  
  16.     
  17.   <body>  
  18.     <h3><s:text name="index_title"/></h3><hr/>  
  19.       
  20.     <s:text name="welcome">  
  21.         <s:param>JavaCrazyer</s:param>  
  22.     </s:text>  
  23.   
  24.   </body>  
  25. </html></span>  

 I18NAction.java

Java代码   收藏代码
  1. <span style="font-size: medium;">package com.javacrazyer.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionContext;  
  4. import com.opensymphony.xwork2.ActionSupport;  
  5.   
  6.   
  7. public class I18NAction extends ActionSupport {  
  8.       
  9.     private static final long serialVersionUID = -7001482935770262132L;  
  10.   
  11.     public String execute() throws Exception{  
  12.           
  13.         String i18nStr = this.getText("welcome"new String[]{"wrr"});  
  14.           
  15.         ActionContext.getContext().put("msg", i18nStr);  
  16.           
  17.         return SUCCESS;  
  18.     }  
  19. }</span>  

 

资源文件:msg_en_US.properties

Java代码   收藏代码
  1. <span style="font-size: medium;">index_title=Struts2 I18N APP Demo  
  2. welcome=Welcome {0}\!</span>  

 

资源文件:msg_zh_CN.properties

Java代码   收藏代码
  1. <span style="font-size: medium;">#这里放置中文消息键/值对  
  2. index_title=Struts2\u56FD\u9645\u5316\u5E94\u7528\u793A\u4F8B  
  3. welcome=\u6B22\u8FCE{0}\u60A8\u7684\u5230\u6765\uFF01</span>  

 

 

src/struts.xml

Html代码   收藏代码
  1. <span style="font-size: medium;"><?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5.   
  6. <struts>  
  7.     <!-- 请求参数的编码方式 -->  
  8.     <constant name="struts.i18n.encoding" value="UTF-8"/>  
  9.     <!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->  
  10.     <constant name="struts.action.extension" value="action,do,go,xkk"/>  
  11.     <!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->  
  12.     <constant name="struts.configuration.xml.reload" value="true"/>  
  13.     <!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->  
  14.     <constant name="struts.devMode" value="false"/>  
  15.     <!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->  
  16.     <constant name="struts.serve.static.browserCache" value="false" />  
  17.     <!-- 是否允许在OGNL表达式中调用静态方法,默认值为false -->  
  18.     <constant name="struts.ognl.allowStaticMethodAccess" value="true"/>  
  19.     <!-- 指定由spring负责action对象的创建   
  20.     <constant name="struts.objectFactory" value="spring" />-->  
  21.       
  22.     <!-- 指定国际化资源文件的基本名 -->  
  23.     <constant name="struts.custom.i18n.resources" value="msg"/>  
  24.       
  25.     <constant name="struts.enable.DynamicMethodInvocation" value="false"/>  
  26.       
  27.     <package name="my" extends="struts-default" namespace="/">  
  28.         <action name="info" class="com.javacrazyer.action.I18NAction">  
  29.             <result>/success.jsp</result>  
  30.         </action>  
  31.     </package>  
  32.       
  33. </struts>  
  34. </span>  

 

succ.jsp

Java代码   收藏代码
  1. <span style="font-size: medium;"><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="/struts-tags" prefix="s" %>  
  3. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  4. <html>  
  5.   <head>  
  6.     <title><s:text name="index_title"/></title>  
  7.     <meta http-equiv="pragma" content="no-cache">  
  8.     <meta http-equiv="cache-control" content="no-cache">  
  9.     <meta http-equiv="expires" content="0">      
  10.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  11.     <meta http-equiv="description" content="This is my page">  
  12.     <!--  
  13.     <link rel="stylesheet" type="text/css" href="styles.css">  
  14.     -->  
  15.   </head>  
  16.     
  17.   <body>  
  18.     <h3><s:property value="#msg"/></h3><hr/>  
  19.   </body>  
  20. </html></span>  

 

 

一般情况下我们浏览器默认语言都是中文,运行呈程序效果如下



 如果更换浏览器语言为英语【美国】,那么重新刷新页面,结果就是英文界面如下


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值