每種框價都會有國際化的支持,struts2的國際化大致上分爲頁面的國際化,Action的國際化以及xml的國際化
首先在struts.properties文件中加入以下內容:
struts.custom.i18n.resources=messageResource
或在struts.xml中加入
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
資源文件的命名格式: 名稱_語言代碼_國家代碼. Properties
如果創建中文和英語國際化,那麼資源文件名稱爲
messageResource_zh_CN.properties和messageResource_en_US.properties
1. jsp頁面的國際化
通過使用標籤<s:text name="label.helloWorld"/>輸出國際化
label.helloWorld爲資源文件中定義的key
在messageResource_en_US.properties加入以下內容
label.hello=hello {0}
label.helloWorld=hello,world
在messageResource_zh_CN.properties加入以下內容
label.hello=你好 {0}
label.helloWorld=你好,世界
(1). <s:text name="label.helloWorld"/>
<s:property value="%{getText('label.helloWorld')}"/>
上面兩個都爲輸出一個hello word的兩種表示
<s:textfield name="name" key="label.helloWorld"/>
<s:textfield name="name" label="%{getText('label.helloWorld')}"/>
顯示一個文本框,文本框的標題進行國際化
(2). 使用<s:i18n>標籤指定從某個特定的資源文件中取數據
<s:i18n name="messageResource">
<s:text name="label.helloWorld"></s:text>
</s:i18n>
指定在從messageResource取資源
(3).
<s:text name="label.hello">
<s:param>callan</s:param>
</s:text>
使用帶參數的資源.<s:param>可以替換label.hello=hello {0}中的{0}
2. Action的國際化
Action的國際化主要是通過getText(String key)方法實現的
- public String execute() throws Exception {
- // getText(String) string爲key
- String str1 = getText("label.helloWorld");
- System.out.println(str1);
- // 帶參數的
- String str2 = getText("label.hello",new String[]{"fjf"});
- System.out.println(str2);
- // 與上一種實現一樣
- List l = new ArrayList();
- l.add("callan");
- String str3 = getText("label.hello",l);
- System.out.println(str3);
- return SUCCESS;
- }
public String execute() throws Exception {
// getText(String) string爲key
String str1 = getText("label.helloWorld");
System.out.println(str1);
// 帶參數的
String str2 = getText("label.hello",new String[]{"fjf"});
System.out.println(str2);
// 與上一種實現一樣
List l = new ArrayList();
l.add("callan");
String str3 = getText("label.hello",l);
System.out.println(str3);
return SUCCESS;
}
3. 參數化國際化
在messageResource_en_US.properties加入以下內容
userName=userName
userName.required=${getText('userName')} is required
在messageResource_zh_CN.properties加入以下內容
userName=用戶名
userName.required=${getText('userName')} 不能爲空
在Action中
String str4 = getText("userName.required");
System.out.println(str4);
userName.required=${getText('userName')}會取國際化的用戶名
4. 使用校驗框價時,提示信息可以國際化
<field name="userName">
<field-validator type="requiredstring">
<message key=”userName.required”> </message>
</field-validator>
</field>
國際化資源文件分爲三種級別
(1) 全局資源文件,可以被整個應該程序引用,也就是struts.custom.i18n.resources=messageResource指定的文件
(2) 包級資源文件,每個包的根目錄下可以新建資源文件,僅被當前包中的類訪問.文件名格式爲:package_語言代碼_國家代碼.
(3) Action級資源文件,僅被當前Action引用,名稱爲action名_語言代碼_國家代碼
查找順序爲從小範圍到大範圍, Action級優先級最大
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>struts.custom.i18n.resources</param-name>
<param-value>globalMessages</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>