Struts2之國際化 .

一、國際化介紹

 

Struts2國際化的目的和Java國際化一致,對於瀏覽器支持的語言顯示不同的文字,如果瀏覽器支持中文,則顯示中文,如果瀏覽器支持英文,則顯示英文;


國際化的目的:

 

很多人剛開始學國際化覺得國際化沒有用,因爲我們通常都只需要顯示中文即可;但是比如google,全世界的人都在訪問此網站,這時如果只顯示中文網頁肯定是不合理的。因此需要國際化;

 

Eclipse for JavaEE和MyEclipse在國際化的優劣:

 

一般來說,資源文件的中文都需要通過native2ascii.exe進行轉換;

Eclipse for JavaEE需要手動轉換;

而MyEclipse可以自動轉換,只需要輸入中文即可;


二、瀏覽器中修改語言

 

 

 

三、資源文件介紹

 

國際化資源文件都是通過Xxx_language_Country.properties 形式;

比如: message_zh_CN.properties、message_en_US.properties;

如果瀏覽器支持中文,則會加載第一個資源文件;

注意:在資源文件中不支持中文,因此需要通過JDK\bin中的native2ascii.exe進行轉換;

在資源文件中內容是以key=value的形式存放的,比如name=xiazdong;

 

四、Struts2國際化

 

 

在Struts2中,在JSP頁面和Action類中都可以使用國際化;

國際化文件分爲:

1.全局國際化資源文件:對於所有Action、JSP都有效,放在WEB-INF\classes下;

2.包範圍國際化資源文件:對於在此包中的Action都有效,放在包路徑下;

3.Action範圍國際化資源文件:對於特定的Action有效,放在Action類的同目錄下;

4.臨時指定國際化資源文件:需要特定語法進行指定纔有效,放在WEB-INF\classes下; 

1.全局國際化資源文件

此文件適用於Action和JSP;

如果需要使用全局資源文件,則需要在struts.xml中添加常量:

<constant name="struts.custom.i18n.resources" value="basename" />

basename是指資源文件的基礎名,比如message_zh_CN.properties的basename是message;

message_zh_CN.properties

[html] view plaincopy
  1. title=\u9875\u9762\u6807\u9898  
  2. body=\u9875\u9762\u5934  
  3. button=\u70b9\u51fb  
  4. name=\u590f\u6b63\u51ac  

message_en_US.properties

[html] view plaincopy
  1. title=Page title  
  2. body=Page body  
  3. button=click  
  4. name=xiazdong  

1.1在JSP中使用國際化

1.1.1一般文本國際化

通過<s:text name="title"/> 可以根據瀏覽器支持的語言取出全局國際化文件的key爲title對應的value;

1.1.2表單控件的國際化

對於表單中的控件,比如按鈕中的文字、textfield前的描述文字,通過指定key屬性進行國際化;

比如<s:textfield key="username"/> 可以在文本域前面的描述中填入username對應的value;

示例代碼:

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=GBK"  
  2.     pageEncoding="GBK"%>  
  3.    <%@taglib prefix="s" uri="/struts-tags"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  8. <title><s:text name="title"/></title>  
  9. </head>  
  10. <body>  
  11.     <s:text name="body"/>  
  12.     <s:form action="aaaa">  
  13.         <s:textfield name="name" key="name"></s:textfield>  
  14.     </s:form>  
  15. </body>  
  16. </html>  

由於本人的瀏覽器默認爲中文,因此顯示的是中文的資源文件;

1.2在Action中使用國際化

在Action中可以使用 getText(String key);這個函數返回國際化文件中key對應的value;

示例代碼:

[java] view plaincopy
  1. package org.locale.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class Demo01Action extends ActionSupport{  
  6.     public String execute()throws Exception{  
  7.         System.out.println(getText("title"));  
  8.         System.out.println(getText("body"));  
  9.         System.out.println(getText("button"));  
  10.         return SUCCESS;  
  11.     }  
  12. }  


1.3 佔位符

 

 

在資源文件中可以使用佔位符,佔位符是指在資源文件中留出一個位置,在使用時動態插入一個任意值;比如在登陸界面時,如果登陸的用戶名是xiazdong,則歡迎界面就會出現xiazdong,歡迎你! 在資源文件中通過 {0},歡迎你!!! 實現;

message_zh_CN.properties

[html] view plaincopy
  1. title=\u9875\u9762\u6807\u9898  
  2. body={0}\u9875\u9762\u5934{1}  
  3. button=\u70b9\u51fb  
  4. name=\u590f\u6b63\u51ac  

 

1.3.1在JSP中填充佔位符

 

在JSP中通過在<s:text>中添加<s:param>進行填充;

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=GBK"  
  2.     pageEncoding="GBK"%>  
  3.    <%@taglib prefix="s" uri="/struts-tags"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  8. <title><s:text name="title"/></title>  
  9. </head>  
  10. <body>  
  11.     <s:text name="body">  
  12.         <s:param>xiazdong</s:param>  
  13.         <s:param>xiazdong</s:param>  
  14.     </s:text>  
  15. </body>  
  16. </html>  

 

1.3.2在Action中填充佔位符

 

在Action中通過 getText(String key, String[] params); 第二個參數進行填充;

[java] view plaincopy
  1. package org.locale.action;  
  2.   
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.   
  5. public class Demo02Action extends ActionSupport{  
  6.     public String execute()throws Exception{  
  7.         System.out.println(getText("body",new String[]{"xiazdong","xiazdong"}));  
  8.         return SUCCESS;  
  9.     }  
  10. }  


2.包範圍資源文件

 

如果一個工程全部的資源文件都在全局國際化資源文件中配置,會很凌亂,因此我們需要進行模塊化,包範圍的資源文件是在包下平進行配置,對於包中所有Action都有效;

包範圍的資源文件只對Action有效,而JSP不能突顯;

包範圍的資源文件的名稱形式爲: package_language_country.properties, 注意 package是常量,比如 package_zh_CN.properties, package_en_US.properties;

如果在包下放置 包範圍資源文件,並且配置全局資源文件,在Action類代碼不變的情況下,則Action類優先加載包範圍的資源文件;

比如目錄結構如圖所示:

則在 org.locale.action包下的Action都優先加載package_****.properties;

 

3.Action範圍資源文件

 

 

Action範圍的資源文件是針對某個特定的Action的,

Action範圍的資源文件的形式: ActionName_language_country.properties; 比如 LoginAction類的資源文件爲:LoginAction_zh_CN.properties;

Action範圍的資源文件優先級比包範圍的資源文件優先級高。

比如目錄結構如圖所示:

則Demo04Action優先加載Demo04Action_****.properties;

 

 

4.利用標籤指定資源文件

 

 

此種方式針對JSP加載指定的資源文件,即可以加載任意範圍的資源文件;

<s:i18n>是指定資源文件的標籤;

在<s:text>等加載資源文件的外面加上<s:i18n name="filename">標籤; filename指定加載的資源文件的名稱;

比如:

[html] view plaincopy
  1. <s:i18n name="message">  
  2. <s:text name="body"/>  
  3.   
  4. <s:form action="aaaa">  
  5.     <s:textfield name="name" key="name"></s:textfield>  
  6. </s:form>  
  7. </s:i18n>  


就可以加載message_**_**.properties 全局資源文件;

目錄結構如下:

接下去將分別在JSP中加載全局範圍、包範圍、Action範圍的資源文件;

4.1 加載全局範圍的資源文件

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=GBK"  
  2.     pageEncoding="GBK"%>  
  3.    <%@taglib prefix="s" uri="/struts-tags"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  8. <title><s:text name="title"/></title>  
  9. </head>  
  10. <body>  
  11.     <s:i18n name="message">  
  12.     <s:text name="body"/>  
  13.       
  14.     <s:form action="aaaa">  
  15.         <s:textfield name="name" key="name"></s:textfield>  
  16.     </s:form>  
  17.     </s:i18n>  
  18. </body>  
  19. </html>  


4.2 加載包範圍的資源文件

加載包範圍的資源文件是,<s:i18n>的name屬性的值爲 包1/包2/package的形式,比如上圖包層次爲 org.locale.action,則爲 org/locale/action/package ;

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=GBK"  
  2.     pageEncoding="GBK"%>  
  3.    <%@taglib prefix="s" uri="/struts-tags"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  8. <title><s:text name="title"/></title>  
  9. </head>  
  10. <body>  
  11.     <s:i18n name="org/locale/action/package">  
  12.     <s:text name="body"/>  
  13.       
  14.     <s:form action="aaaa">  
  15.         <s:textfield name="name" key="name"></s:textfield>  
  16.     </s:form>  
  17.     </s:i18n>  
  18. </body>  
  19. </html>  


4.3加載action範圍的資源文件

加載Action範圍的資源文件時,<s:i18n>的name屬性值的形式爲: 包層次/ActionName,比如 org/locale/action/Demo04Action 則加載Demo04Action的資源文件;

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=GBK"  
  2.     pageEncoding="GBK"%>  
  3.    <%@taglib prefix="s" uri="/struts-tags"%>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  8. <title><s:text name="title"/></title>  
  9. </head>  
  10. <body>  
  11.     <s:i18n name="org/locale/action/Demo04Action">  
  12.     <s:text name="body"/>  
  13.       
  14.     <s:form action="aaaa">  
  15.         <s:textfield name="name" key="name"></s:textfield>  
  16.     </s:form>  
  17.     </s:i18n>  
  18. </body>  
  19. </html>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章