前提条件:strut2的必须架包已经引入,struts2的配置文件全部配置完成
1.首先创建一个struts.properties在src目录下,服务器启动时候会自动加载该文件,在这个文件中写入struts.custom.i18n.resources=globalMessages,服务器会自动选择前缀为globalMessages资源文件。(或者你在你的struts.xml文件中添加<constant name="struts.custom.i18n.resources" value="globalMessages"></constant>服务器加载struts.xml的时候会加载国际化资源文件);
2.在src目录下创建资源文件英文globalMessages_en.properties 中文globalMessages_zh.properties。
globalMessages_en.properties 文件内容为home=home
globalMessages_zh.properties文件内容为 home=\u9996\u9875
3.在页面端获得资源文件信息。<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'test.jsp' starting page</title>
</head>
<body>
<s:text name="home"></s:text>
</body>
</html>
struts2的国际化分三种情况
1.前台页面的国际化,
2.Action类中的国际化,
3.配置文件的国际化。
首先指定全局的国际化资源文件:
在配置文件struts.xml中引入
<constant name="struts.custom.i18n.resources" value="message"></constant>(注意位置)
或
在struts.properties文件中指定如下一行:
struts.custom.i18n.resources=message
指定的国家化资源文件即为
xxx_语言_国家.properties
message_zh_CN.properties(简体中文资源文件)
message_en_US.properties(美国英语资源文件)
(1).JSP页面上的国际化(使用struts2的<s:text .../>):
<s:i18n name="message">
<s:text name="hello">
<s:param>${username}</s:param>
</s:text>
</s:i18n>
message_en_US.properties文件配置:
hello=hello world,{0}
message_zh_CN.properties文件配置:
hello=你好,{0}
(2)表单元素的Label国际化:
未国际化:
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="password" label="password"></s:textfield>
国际化后:
<s:textfield name="username" key="uname"></s:textfield>
<s:textfield name="password" key="pword"></s:textfield>
message_en_US.properties文件,配置:
uname=username
pword=password
message_zh_CN.properties文件,配置:
uname=用户名
pword=密码
(3).Action中的国际化:
未国际化:
this.addFieldError("username", "the username error!");
this.addFieldError("password", "the password error!");
国际化后:
this.addFieldError("username", "username.error");
this.addFieldError("password", "password.error");
message_en_US.properties文件配置:
username.error = the username error !
password.error = the password error!
message_zh_CN.properties文件配置:
username.error=用户名错误!
username.error=密码错误!
(4).配置文件中的国际化:
以输入校验的LoginAction-validation.xml为例:
未国际化:
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username should not be empty!</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">12</param>
<message>username should be between ${minLength} and ${maxLength}!</message>
</field-validator>
</field>
国际化后:
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message key="username.empty !"></message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">12</param>
<message key="username.size"></message>
</field-validator>
</field>
message_en_US.properties文件配置:
username.empty = the username should not be empty !
username.size = the size of username shoule be between 6 and 12 !
message_zh_CN.properties文件配置:
username.empty =用户名不能为空 !
username.size = 用户名长度在6到12 !
注:message_zh_CN.properties这个国际化资源文件不允许包含非西欧字符。
Java提供了一个工具来处理该文件中的中文:native2ascii,这个工具可以在%JAVA_HOME%/bin路劲下找到。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yaa2004/archive/2009/10/10/4650796.aspx