Struts2温习(9)--国际化的应用

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

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title><s:text name="index_title"/></title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <h3><s:text name="index_title"/></h3><hr/>
    
    <s:text name="welcome">
    	<s:param>JavaCrazyer</s:param>
    </s:text>

  </body>
</html>

 I18NAction.java

package com.javacrazyer.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class I18NAction extends ActionSupport {
	
	private static final long serialVersionUID = -7001482935770262132L;

	public String execute() throws Exception{
		
		String i18nStr = this.getText("welcome", new String[]{"wrr"});
		
		ActionContext.getContext().put("msg", i18nStr);
		
		return SUCCESS;
	}
}

 

资源文件:msg_en_US.properties

index_title=Struts2 I18N APP Demo
welcome=Welcome {0}\!

 

资源文件:msg_zh_CN.properties

#这里放置中文消息键/值对
index_title=Struts2\u56FD\u9645\u5316\u5E94\u7528\u793A\u4F8B
welcome=\u6B22\u8FCE{0}\u60A8\u7684\u5230\u6765\uFF01

 

 

src/struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
    "http://struts.apache.org/dtds/struts-2.1.7.dtd">

<struts>
	<!-- 请求参数的编码方式 -->
	<constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 指定被struts2处理的请求后缀类型。多个用逗号隔开 -->
	<constant name="struts.action.extension" value="action,do,go,xkk"/>
	<!-- 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开  -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开  -->
	<constant name="struts.devMode" value="false"/>
	<!-- 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭  -->
	<constant name="struts.serve.static.browserCache" value="false" />
	<!-- 是否允许在OGNL表达式中调用静态方法,默认值为false -->
	<constant name="struts.ognl.allowStaticMethodAccess" value="true"/>
	<!-- 指定由spring负责action对象的创建 
	<constant name="struts.objectFactory" value="spring" />-->
	
	<!-- 指定国际化资源文件的基本名 -->
	<constant name="struts.custom.i18n.resources" value="msg"/>
	
	<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
	
	<package name="my" extends="struts-default" namespace="/">
		<action name="info" class="com.javacrazyer.action.I18NAction">
			<result>/success.jsp</result>
		</action>
	</package>
	
</struts>

 

succ.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title><s:text name="index_title"/></title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
    <h3><s:property value="#msg"/></h3><hr/>
  </body>
</html>

 

 

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



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



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值