一、介紹
國際化Internationalization的首末字符I和N之間字符數是18,簡稱I18N
“國際化”是指一個應用程序在運行時,能夠根據客戶端請求所來自國家或地區語言的不同,而顯示不同的語言界面。
比如:你的電腦是中文操作系統,這個應用程序的界面使用中文文字;客戶端是英文操作系統,就顯示英文界面。
國際化資源文件,后綴以properties作為擴展名,該文件以“鍵=值”(key=value)對的形式存儲資源數據
二、命名格式:
1、資源文件名.properties
2、資源文件名_語言種類.properties
三、編碼
在國際化時,所有的字符都要使用標准的編碼方式,需要把中文字符轉換為Unicode代碼。
中文資源文件不能直接使用,必須轉換為Unicode代碼
Eclipse中,在globalMessages_zh_CN.properties文件中,在等式右邊輸入中文,自動轉換為Unicode編碼方式,如圖所示:

三、應用實例
登陸時調用中文資源文件如下:

IE瀏覽器將英文[en-US]上移到頂部,如下:

重新打開瀏覽器登陸頁面,此時調用英文資源文件,如下

Eclipse中實例目錄結構,如下:

1、編寫國際化資源文件,將這兩個文件放在和struts.xml同一個目錄
中文文件:globalMessages_zh_CN.properties

英文文件:globalMessages_en_US.properties

2、web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <!-- 配置Struts2核心Filter的名字 --> <filter-name>struts2</filter-name> <!-- 配置Struts2核心Filter的實現類 --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 配置Filter攔截的URL --> <filter-mapping> <!-- 配置Struts2的核心FilterDispatcher攔截所有用戶請求 --> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
3、編寫視圖組件
(1)login.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ 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> <!-- 使用text標簽輸出國際化消息 --> <title><s:text name="loginTitle" /></title> </head> <body> <s:form action="checkLogin" method="post"> <!-- 表單元素的key值與資源文件的key對應 --> <s:textfield name="name" key="loginName"></s:textfield> <s:password name="password" key="loginPassword"></s:password> <s:submit key="loginSubmit"></s:submit> </s:form> </body> </html>
(2)loginSuccess.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ 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> <!-- 使用text標簽輸出國際化消息 --> <title><s:text name="successPage" /></title> </head> <body> <hr> <s:text name="loginName" />:<s:property value="name" /><br> <s:text name="loginPassword" />:<s:property value="password" /> </body> </html>
4、編寫業務控制器
LoginAction1.java
package loginAction; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction1 extends ActionSupport{ private String name; private String password; //用於定義標題信息 private String tip; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getTip() { return tip; } public void setTip(String tip) { this.tip = tip; } public String execute() throws Exception{ if(getName().equals("QQ")&&getPassword().equals("123")){ ActionContext.getContext().getSession().put("name", getName()); return SUCCESS; }else{ return ERROR; } } }
5、在struts.xml中配置Action和國際資源文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 使用Struts2中的I18N攔截器,並通過constant元素配置常量,指定國際資源文件名字, value的值就是“資源文件名” --> <constant name="struts.custom.i18n.resources" value="globalMessages" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="I18N" extends="struts-default"> <action name="checkLogin" class="loginAction.LoginAction1"> <result name="success">/I18N/loginSuccess.jsp</result> <result name="error">/I18N/login.jsp</result> </action> </package> </struts>
