struts2国际化——实例
首先建一个strut2的项目这就不多说!
1.在struts.properties中加入如下代码
#国际化文件
struts.custom.i18n.resources=messageResources(这个是资源文件的前缀,如果要是有文件目录就这样写xxxx.xxx...messageResources)
或者在struts.xml里面加上
<constant name="struts.custom.i18n.resources" value="messageResources"></constant>
2.建一个languageAction
package com.hopsun.strust2.action;
import java.util.Locale;
import com.opensymphony.xwork2.ActionContext;
public class InternationalAction {
private String languageName;
//这个是测试直接用request_locale=zh_CN/*
public String ChangeLanguage() {
return "success";
}
//这个是测试 用我们自定义一个页面参数,更换语言类型
public String ChangeLanguage2() {
Locale l = Locale.getDefault();
if(this.languageName==null){
l = new Locale("zh", "CN");
}else if (this.languageName.equals("ft")) {
l = new Locale("zh", "TW");
} else if (this.languageName.equals("en")) {
l = new Locale("en", "US");
}
// ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE",
// l);
ActionContext.getContext().setLocale(l);
return "success";
}
public String getLanguageName() {
return languageName;
}
public void setLanguageName(String languageName) {
this.languageName = languageName;
}
}
3.把action交给spring来管理
<bean name="Inter" scope="prototype" />
4.struts.xml配置
<action name="Change" method="ChangeLanguage">
<result>/Internationalization.jsp</result>
</action>
<action name="Chan" method="ChangeLanguage2">
<result>/Internationalization.jsp</result>
</action>
5.资源文件
放在classes下面,即项目/src/
messageResources_zh_CN.properties
#messageResources_zh_CN.properties
language=语言
name=姓名
age=年龄
sex=性别
messageResources_en_US.properties
#messageResources_en_US.properties
language=language(re)
name=name(re)
age=age(re)
sex=sex(re)
messageResources_zh_TW.properties
#messageResources_zh_TW.properties
language=語言
name=姓名
age=年齡不是問題
sex=性別
6.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'Internationalization.jsp' starting page</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">
</head>
<body>
<!-- 测试第一种 对应action 方法ChangeLanguage() request_locale-->
<a href="<%=basePath%>Change.do?request_locale=zh_CN">中文简体</a>
<a href="<%=basePath%>Change.do?request_locale=zh_TW">中文繁体</a>
<a href="<%=basePath%>Change.do?request_locale=en_US">English</a><br>
<!-- 测试测试自定义参数 对应action 方法ChangeLanguage2() languageName-->
<a href="<%=basePath%>Change.do">中文简体</a>
<a href="<%=basePath%>Change.do?languageName=ft">中文繁体</a>
<a href="<%=basePath%>Change.do?languageName=en">English</a><br>
<s:label key="language"/><input name="language">
<s:label key="name"/><input name="name">
<s:label key="age"/><input name="age">
<s:label key="sex"/><input name="sex">
</body>
</html>