struts2-國際化
國際化:使程式在不做任何修改的情況下,就可以使用在不同的語言環境中。
i18n(Internationalization)
國際化最重要的就是資原始檔,在執行是對資原始檔的內容呼叫,而資原始檔作用範圍有:
- 全域性範圍
- 包範圍
- action範圍
struts2中國際化實現簡單例項:(先貼程式碼,後面分析)
1、test.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/test/lan.action?request_locale=zh_CN" >中文</a>
<a href="${pageContext.request.contextPath }/test/lan.action?request_locale=en_US">English</a>
<s:form action="test/some" method="post">
<s:textfield name="name" key="test.form.name"></s:textfield>
<s:textfield name="password" key="test.form.password" ></s:textfield>
<s:submit key="test.form.submit"></s:submit>
</s:form>
</body>
</html>
2、welcome.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:text name="welcome.text.msg">
<s:param><s:property value="name"/></s:param>
</s:text>
${sessionScope.msg}<br>
</body>
</html>
3、struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="my"></constant>
<package name="test" namespace="/test" extends="struts-default" >
<action name="some" class="com.test2.loginAction" >
<result>/welcome.jsp</result>
</action>
<action name="lan">
<result>/test.jsp</result>
</action>
</package>
</struts>
4、loginAction.java
public class loginAction extends ActionSupport {
private String name;
private int password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
public String execute(){
// String message = this.getText("action.login.message");
String[] args = {name};
String message = this.getText("action.login.message",args);
ActionContext.getContext().getSession().put("msg", message);
System.out.println("loginAction");
return "success";
}
}
5、全域性資原始檔my_en_US.properties
test.form.name=name
test.form.password=password
test.form.submit=login
action.login.message=System!{0}
welcome.text.msg=welcome!{0}
6、全域性資原始檔
test.form.name=\u59D3\u540D
test.form.password=\u5BC6\u7801
test.form.submit=\u767B\u5F55
action.login.message=\u7CFB\u7EDF{0}
welcome.text.msg=\u6B22\u8FCE{0}
因為東西太雜,以一個總體程式碼展示,以下詳細解析:
1、先以正常的方式寫一個簡單表單:
<form action="test/some" method="post">
name : <input name="name" type="text"><br>
password: <input name="password" type="text"><br>
<input type="submit" value="submit">
</form>
- 這裡面的name,password,submit在現實時是需要國際化的,於是編寫對應的資原始檔。
- 檔案內容是鍵值對,這裡的key都是任意的,內容則是對應著需要國際化的內容。
- 有多少種語言就寫幾種資原始檔。
2、在struts2中對資原始檔內容的引用要通過< s:>標籤來實現,所以將需要國際化的內容都轉換成此標籤對應的內容:(這裡的key就是資原始檔中定義key值)
<s:form action="test/some" method="post">
<s:textfield name="name" key="test.form.name"></s:textfield>
<s:textfield name="password" key="test.form.password"></s:textfield>
<s:submit key="test.form.submit"></s:submit>
</s:form>
3、對於資原始檔,全域性範圍的資原始檔:
- 命名隨意,但一般以xxx_en_US.propertis這種形式
- 需要在struts.xml中註冊
- 註冊後預設讀取的資源就是這個全域性資原始檔
4、於是再進行資原始檔註冊(給struts.custom.i18n.resources常量賦值,資原始檔名因為前面有一樣的命名內容,所以可以省略後面的,讓struts2自動匹配)
<constant name="struts.custom.i18n.resources" value="my"></constant>
5、最後要實現在轉換語言時,為了能呼叫相應的資原始檔,就需要通過action來實現。
在預設的Action Support類中,會執行預設攔截器棧(裡面有i18n攔截器)。
所以就在struts.xml中註冊處理國際化請求的action(action Support是預設執行的action,可以省略)
<action name="lan">
<result>/test.jsp</result>
</action>
6、在頁面新增國際化請求:
<a href="${pageContext.request.contextPath }/test/lan.action?request_locale=zh_CN">中文</a>
<a href="${pageContext.request.contextPath }/test/lan.action?request_locale=en_US">English</a>
在action請求後要加request_locale引數並賦值,這個引數在i18n攔截器中定義著(可看原始碼)
到這裡就實現了簡單的靜態頁面國際化,當國際化的內容裡有動態引數時,需要為動態引數新增佔位符:
action.login.message=System!{0}
welcome.text.msg=welcome!{0}
action.login.message=System!{0}這裡的動態引數賦值,在action方法中進行,如:
// String message = this.getText("action.login.message");呼叫資原始檔對應的靜態值
String[] args = {name};//將name放入String數值中
String message = this.getText("action.login.message",args);//為資原始檔的佔位符賦值並取出value
ActionContext.getContext().getSession().put("msg", message);//將取出的內容放入Session中,以便跳轉頁面中呼叫顯示資料
welcome.text.msg=welcome!{0}中動態引數的賦值在jsp頁面中進行:
<s:text name="welcome.text.msg">
<s:param><s:property value="name"/></s:param>
</s:text>
包範圍資原始檔
- 命名如:package_en_US.properties,package是規定的
- 命名被規範,所以不用註冊
- 在進去相應的包內才會獲取該資源,如執行action時取該包範圍資源
- 放在包下
action範圍資原始檔
- 命名如:Action類名_en_US.properties
- 命名被規範,所以不用註冊
- 在執行相應的action才會獲取該資源
- 放在action包對應包下
這些資原始檔,會按範圍縮小,相互覆蓋
在jsp中可以自定義使用資原始檔
<s:i18n name="my"></s:i18n>//這裡name賦值資源路徑
//將國際化的標籤,包含進來
<s:form action="test/some" method="post">
<s:textfield name="name" key="test.form.name"></s:textfield>
<s:textfield name="password" key="test.form.password"></s:textfield>
<s:submit key="test.form.submit"></s:submit>
</s:form>
<s:i18n name="com/test2/loginAction"></s:i18n>