对于struts2这么优秀的框架,对于国际化的问题也有很多种类型,全局的,专对于action,还有对于一个包的,还有最弱的临时i18n文件。在这不废话了,就写写最基本的全局的i18n文件。
struts.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="Testi18n" extends="struts-default">
<action name="zhuce" class="com.zq.TestAction">
<result name="success">out.jsp</result>
</action>
</package>
</struts>
input.jsp用来输入的页面
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="s"uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<h1><s:text name="title"></s:text></h1>
<hr>
<s:form action="zhuce">
<s:textfield name="username" key="username"></s:textfield>
<s:textfield name="passwd" key="passwd"></s:textfield>
<s:submit key="submit"></s:submit>
</s:form>
</body>
</html>
out.jsp用来显示的页面 前面一部分只是为了验证已得到值,后一部分使用的国际化文件
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="s"uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
姓名为:<s:property value="username"/>
密码为:<s:property value="passwd"/><hr>
<s:text name="message">
<s:param>
<s:property value="username"/>
</s:param>
<s:param>
<s:property value="passwd"/>
</s:param>
</s:text>
</body>
</html>
TestAction 一个简单的action没有数据的输入校验,也没有类型转换的自定义问题
package com.zq;
import com.opensymphony.xwork2.ActionSupport;
public class TestAction extends ActionSupport {
private String username;
private String passwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
@Override
public String execute() throws Exception {
return "success";
}
}
接下来是i18n文件的定义 大管家struts.properties
struts.custom.i18n.resources=zqResource
中文的文件:zqResourc_zh_CN.properties 中文在该模式下是不能保存的,保存的也是乱码,所以需要使用native2ascii命令来处理
username=\u7528\u6237\u540d
passwd=\u5bc6\u7801
submit=\u63d0\u4ea4
title=\u6d4b\u8bd5
message=\u6b22\u8fce{0}\u60a8\u7684\u5bc6\u7801\u4e3a{1}\u8bf7\u6ce8\u610f\u4fdd\u5b58
英语的文件:zqResourc_en_US.properties
username=username
passwd=passwd
submit=submit
title=Test
message=Welcome {0} Your password is {1} Please rember it
这就是一个简单的国际化的问题,相信大家一看就懂。还有个语言转换的链接没写。请大家多多指教。