第一步:新建一个jsp页面,代码如下:
<%@ page language="java" pageEncoding="GBK"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<s:i18n name="com.wkx.action.I18NTest">
<html>
<head>
<title><s:text name="title"/></title>
</head>
<body>
<s:a href="i18n.action?type=zh_CN"><s:text name="link1"/></s:a>
<s:a href="i18n.action?type=zh_TW"><s:text name="link2"/></s:a>
<s:a href="i18n.action?type=en_US"><s:text name="link3"/></s:a><br/><br/>
<s:text name="save"></s:text><br/><br/>
<s:text name="delete"></s:text><br/><br/>
<s:text name="msg"></s:text>
</body>
</html>
</s:i18n>
注意: <s:i18n>标记可以把html标记包含起来,其中name属性值要包含资源文件的全路径
I18NTest表示资源文件名的前半部分,后半部分是语言类型。
第二步:建立action类
package com.wkx.action;
import java.util.Locale;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class I18NTest extends ActionSupport {
private String type;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String execute() throws Exception{
Locale local = Locale.getDefault();
if(this.type==null){
local = new Locale("zh","CN");
}else if("zh_CN".equals(this.type)){
local = new Locale("zh","CN");
}else if("en_US".equals(this.type)){
local = new Locale("en","US");
}else if("zh_TW".equals(this.type)){
local = new Locale("zh","TW");
}
ServletActionContext.getContext().setLocale(local);
return "success";
}
}
struts配置如下:
<action name="i18n" class="com.wkx.action.I18NTest">
<result>/i18n.jsp</result>
</action>
第三步:创建资源文件,代码在下面附件里面