Posted on 2008-08-24 01:21
∪∩BUG 阅读(2573)
评论(4) 编辑 收藏 所属分类:
Struts2学习笔记
Struts 笔记
2008 08 23
为什么把国际化通称I18n呢? 你数数国际化的单词 internationalization有几个字母? 20个! 那头尾各是什么字母? i和n! 20-2=? 18! 那好请记下:I18n
一.标准i18n(国际化)操作
(1)添加资源文件的方式之一:在struts.xml文件中引入资源文件 <constant name="struts.custom.i18n.resources" value="资源文件名" />
添加资源文件的方式之二:创建struts.properties并在添加语句struts.custom.i18n.resources=资源文件名
(2)在JSP页面中调用资源文件的方法:
1) 使用<s:text key / label/>
如:<s:text name="HelloWorld" />
2) 使用<s:property >
如:
<!-- 1.取当前项目文件中的key(name)叫做HelloWorld
2.使用EL表达式通过getText方法简单的获取国际化字符串-->
<s:property value="%{getText('HelloWorld')}" />
<!-- 通过getText设置UI标签中label标签的属性 -->
<s:textfield name="name" label="%{getText('HelloWorld')}" />
(3)资源文件不同配置的自动查找顺序
第一:通过<s:i18n>访问待定位置的资源文件
第二:Action范围的资源文件(只能由Action调用),资源文件名在Action的类文件的同级目录下,与Action类名相同.
第三:包范围内的资源文件(以模块方式管理资源文件),放在包目录下.以package_en_zh
第四:通用性较强的全局资源文件,放在SRC目录下
项目树型图

src/struts.xml
1
<?xml version="1.0" encoding="GBK"?>
2
<!DOCTYPE struts PUBLIC
3
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4
"http://struts.apache.org/dtds/struts-2.0.dtd">
5
<struts>
6
<!--
7
Struts 2.0有两个配置文件,struts.xml和struts.properties都是放在WEB-INF/classes/下。
8
struts.xml用于应用程序相关的配置
9
struts.properties用于Struts 2.0的运行时(Runtime)的配置
10
11
添加资源文件的方式之一:在struts.xml文件中引入资源文件
12
添加资源文件的方式之二:在struts.properties下添加语句struts.custom.i18n.resources=globalMessages
13
1.在Struts当中用户自定义的国际化资源文件,名为globalMessages.
14
2.value="globalMessages"没有指定路径,说明属性文件在SRC目录下,此为全局资源文件
15
-->
16
<constant name="struts.custom.i18n.resources"
17
value="globalMessages" />
18
<include file="struts-default.xml" /><!-- 使用缺省的struts的配置文件 -->
19
20
<!-- 包空间 I18nDemo 继承 struts-default -->
21
<package name="I18nDemo" extends="struts-default">
22
23
<!-- 映射名name="LoginAction" ,使用com.Login.LoginAction来实现 -->
24
<action name="LoginAction" class="com.Login.LoginAction">
25
<result>/LoginAction.jsp</result>
26
</action>
27
</package>
28
</struts>
29
30
WebRoot/WEB-INF/web.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<web-app id="WebApp_ID" version="2.4"
3
xmlns="http://java.sun.com/xml/ns/j2ee"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
6
<display-name>Struts2Hello</display-name>
7
<filter>
8
<filter-name>struts2</filter-name>
9
<filter-class>
10
org.apache.struts2.dispatcher.FilterDispatcher
11
</filter-class><!-- 以过虑器的形式出现 -->
12
</filter>
13
<filter-mapping>
14
<filter-name>struts2</filter-name>
15
<url-pattern>/*</url-pattern><!-- 过虑所有内容 -->
16
</filter-mapping>
17
<welcome-file-list>
18
<welcome-file>index.html</welcome-file>
19
<welcome-file>index.htm</welcome-file>
20
<welcome-file>index.jsp</welcome-file>
21
<welcome-file>default.html</welcome-file>
22
<welcome-file>default.htm</welcome-file>
23
<welcome-file>default.jsp</welcome-file>
24
</welcome-file-list>
25
</web-app>
26
WebRoot/index.jsp
1
<%
@ page contentType="text/html; charset=UTF-8"%>
2
<%
@taglib prefix="s" uri="/struts-tags"%>
3
<html>
4
<head>
5
<title>Hello World</title>
6
</head>
7
<body>
8
<h2>
9
<s:text name="HelloWorld" />
10
</h2>
11
<h2>
12
<!--
13
1.取当前项目文件中的key(name)叫做HelloWorld
14
2.使用EL表达式通过getText方法简单的获取国际化字符串
15
-->
16
<s:property value="%{getText('HelloWorld')}" />
17
</h2>
18
<!-- 通过getText设置UI标签中label标签的属性 -->
19
<s:textfield name="name" label="%{getText('HelloWorld')}" />
20
</body>
21
</html>
22
23
WebRoot/LoginAction.jsp
1
<%
@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2
<%
@ page contentType="text/html; charset=GBK" %>
3
<%
4
String path = request.getContextPath();
5
String basePath = request.getScheme() + "://"
6
+ request.getServerName() + ":" + request.getServerPort()
7
+ path + "/";
8
%>
9
<%
@taglib prefix="s" uri="/struts-tags"%>
10
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
11
<html>
12
<head>
13
<title>Login Action</title>
14
</head>
15
<!-- 用http://localhost:8080/i18n/LoginAction.action?uname=admin&upass=123显示页面 -->
16
<body>
17
<!--
18
1.包范围内的资源文件(以模块方式管理资源文件)
19
2.输出请求文件当中tip,user的值.(在LoginAction中已经被放入值)
20
-->
21
${requestScope.tip}
22
<br>
23
${sessionScope.user}
24
<br>
25
26
<!--
27
1.Action范围的资源文件(只能由Action调用)
28
2.输出标准的request的tip属性
29
-->
30
<jsp:useBean id="d" class="java.util.Date" scope="page" />
31
<!-- s:text name="msg"直接从资源文件中取值({0},{1}两个值)并显示 -->
32
<s:text name="msg">
33
34
<%
35
//给msg的{0}携带值
36
%>
37
<s:param>
38
<s:property value="uname" />
39
</s:param>
40
41
<%
42
//给msg的{1}携带值
43
%>
44
<s:param>${d}</s:param>
45
</s:text>
46
47
<!-- 通过s:i18n访问待定位置的资源文件,此时只访问globalMessages -->
48
<s:i18n name="globalMessages">
49
<s:text name="HelloWorld" />
50
</s:i18n>
51
<br>
52
<!-- 通用性较强的全局资源文件 -->
53
<s:property value="%{getText('HelloWorld')}" />
54
<br>
55
<hr>
56
<!-- 包含 LangSelector.jsp页面-->
57
<s:include value="LangSelector.jsp"/>
58
</body>
59
</html>
60
WebRoot/LangSelector.jsp
1
<%
@ page language="java" contentType="text/html; charset=GBK"%>
2
3
<%
@taglib prefix="s" uri="/struts-tags"%>
4
5
<!-- 提交表单的方法 -->
6
<script type="text/javascript">
7
function langSelecter_onChanged()
8

{
9
document.getElementById("langForm").submit();
10
}
11
</script>
12
13
14
<!--
15
1.s:set:将用户的的session中的WW_TRANS_I18N_LOCALE设置成SESSION_LOCALE,(SESSION_LOCALE已经存在)
16
2.s:bean:执行com.locales.Locales类,将内容为SESSION_LOCALE得到的值传入类中的参数current,
17
传值时如果值为空则选择为locale(本地)否则选择为SESSION_LOCALE
18
-->
19
<s:set name="SESSION_LOCALE" value="#session['WW_TRANS_I18N_LOCALE']" />
20
<s:bean id="locales" name="com.locales.Locales">
21
<s:param name="current"
22
value="#SESSION_LOCALE == null?locale:#SESSION_LOCALE" />
23
</s:bean>
24
25
<!--
26
1.action="<s:url/>":提交给当前的地址
27
2.s:text:获取资源文件(这里得到的是全局资源文件的里的Key)
28
3.list="#locales.locales":通过s:bean id="locales"调用Locales.java中的getLocales()方法
29
-->
30
<form action="<s:url/>" id="langForm"
31
style="background-color: #bbbbbb; padding-top: 4px; padding-bottom: 4px;">
32
<s:text name="languag" />
33
<s:select label="Language" list="#locales.locales" listKey="value"
34
listValue="key"
35
value="#SESSION_LOCALE == null ? locale : #SESSION_LOCALE"
36
name="request_locale" id="langSelecter"
37
onchange="langSelecter_onChanged()" theme="simple" />
38
</form>
39
src/com/package_en_US.properties
succtip=Login success
failtip=Login failed

src/com/package_zh_CN.properties
succtip=\u767B\u5F55\u6210\u529F
failtip=\u767B\u5F55\u5931\u8D25

src/com.i18n.Loc.java
1
package com.i18n;
2
3
import java.util.Locale;
4
5
/** *//**
6
* @author ∪∩BUG E-mail: tidelgl@163.com
7
* @version Aug 23, 2008 3:37:45 PM
8
* @获取语言环境
9
*/
10
public class Loc
{
11
public static void main(String[] args)
{
12
Locale[] localelist = Locale.getAvailableLocales();
13
for (int i = 0; i < localelist.length; i++)
{
14
// 打印国家名+国家编写名+语言名+语言缩写名
15
System.out.println(localelist[i].getDisplayCountry() + " = "
16
+ localelist[i].getCountry() + " "
17
+ localelist[i].getDisplayLanguage() + " = "
18
+ localelist[i].getLanguage());
19
}
20
// 获取系统的默认的国家/语言环境
21
// Locale myLocale = Locale.getDefault();
22
// ResourceBundle bundle = ResourceBundle.getBundle("资源文件名", myLocale);
23
// System.out.println(bundle.getString("定义的Key键"));
24
}
25
26
}
27
src/com.locales.Locales.java
1
package com.locales;
2
3
import java.util.Hashtable;
4
import java.util.Locale;
5
import java.util.Map;
6
7
/** *//**
8
* @author ∪∩BUG E-mail: tidelgl@163.com
9
* @version Aug 23, 2008 11:27:29 PM
10
* @本地化
11
*/
12
public class Locales
{
13
private Locale current;
14
15
// 设置当前用户的语言种类
16
public void setCurrent(Locale current)
{
17
this.current = current;
18
}
19
20
public Map<String, Locale> getLocales()
{
21
Map<String, Locale> locales = new Hashtable<String, Locale>();
22
23
// 往集合中装载语言种类
24
locales.put("USA English", Locale.US);
25
locales.put("Simplified Chinese", Locale.CHINA);
26
return locales;
27
28
}
29
}
30
src/com.Login.LoginAction.java
1
package com.Login;
2
3
import com.opensymphony.xwork2.ActionContext;
4
import com.opensymphony.xwork2.ActionSupport;
5
6
/** *//**
7
* @author ∪∩BUG E-mail: tidelgl@163.com
8
* @version Aug 23, 2008 8:39:58 PM
9
* @国际化
10
*/
11
public class LoginAction extends ActionSupport
{
12
private String uname;
13
private String upass;
14
15
public LoginAction(String uname, String upass)
{
16
super();
17
this.uname = uname;
18
this.upass = upass;
19
}
20
21
public LoginAction()
{
22
23
}
24
25
public String getUname()
{
26
return uname;
27
}
28
29
public void setUname(String uname)
{
30
this.uname = uname;
31
}
32
33
public String getUpass()
{
34
return upass;
35
}
36
37
public void setUpass(String upass)
{
38
this.upass = upass;
39
}
40
41
@Override
42
public String execute() throws Exception
{
43
44
ActionContext ctx = ActionContext.getContext();// 获取struts2的整个上下文对象,这里是request
45
if ("admin".equals(this.uname) & "123".equals(this.getUpass()))
{
46
// ctx.getSession().put("user", this.getUname()); // 把当前的用户名放入session当中.
47
// ctx.put("tip", this.getText("succtip")); // Action当中获取资源文件的方法,这里取的key名为succtip
48
49
//占位的资源文件的获取
50
ctx.getSession().put("user", this.getUname()); // 把当前的用户名放入session当中.
51
ctx.put("tip", this.getText("succtip",new String[]
{this.uname})); //在字符串数组中的第一个数组元素放入uname的值
52
} else
{
53
// ctx.put("tip", this.getText("failtip")); // Action当中获取资源文件的方法,这里取的key名为failtip
54
ctx.put("tip", this.getText("failtip",new String[]
{this.uname})); //在字符串数组中的第一个数组元素放入uname的值
55
}
56
57
return SUCCESS;
58
}
59
60
}
61
src/com.locales.LoginAction_en_US.properties

succtip=
{0}Login success

failtip=
{0}Login failed

msg=
{0}welcome,now is\:
{1}

src/com.locales.LoginAction_zh_CN.properties
1
succtip=
{0}\u767B\u5F55\u6210\u529F
2
failtip=
{0}\u767B\u5F55\u5931\u8D25
3
msg=
{0}\u6B22\u8FCE,\u73B0\u5728\u662F\:
{1}
4
src/globalMessages_en_US.properties
HelloWorld=Hello World\!
com.locales=Test Locale
src/globalMessages_zh_CN.properties
HelloWorld=\u4F60\u597D,\u4E16\u754C\!
com.locales=\u672C\u5730\u6D4B\u8BD5

src/struts.properties
struts.custom.i18n.resources=globalMessages