Struts2國際化介紹及詳細說明

 

Struts2國際化介紹及詳細說明


  

Struts2國際化介紹及詳細說明

                                                                                                                

     我們來了解一下這個國際化:國際化是在軟件設計和文檔開發過程中,使得功能和代碼設計能處理多種語言和文化習俗,在創建不同語言版本時,不需要重新設計源程序代碼的軟件工程方法。

      我們看struts2的國際化,首先在struts2的國際化,我們應必備的前提,首先如果我們要使用struts2的國際化,我們首先要在struts2中配置一個標籤如:<constantname="struts.custom.i18n.resources"value="csdn"></constant>,這樣我們就能使用struts2中的國際化了,但是這個配置必須的是你在你的struts2配置環境能用的基礎上.

       我們在看具體的使用:

struts2的國際化分三種情況,同時也分三個範圍,

1.前臺頁面的國際化,

2.Action類中的國際化,

3.配置文件的國際化。

       我們首先來看全局的國際化,

1>前臺國際化:

       我們首先配置一下struts.xml文件中的配置,我們寫一個例子:

如:

[html] view plain copy
  1. <!DOCTYPE struts PUBLIC  
  2.   
  3.    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"  
  4.   
  5.    "http://struts.apache.org/dtds/struts-2.3.dtd">  
  6.   
  7.    <struts>  
  8.   
  9.    <constant name="struts.custom.i18n.resources" value="csdn"></constant>  
  10.   
  11.    <include file="struts-validate.xml"></include>  
  12.   
  13.    <package name="a" extends="struts-default" namespace="/">  
  14.   
  15.    </package>  
  16.   
  17.    </struts>  


解釋一下,這個引入的標籤名字是contant,這個name就是他的屬性,在struts2原文件中的properties中可以找到,然後value就是他開始的前綴名字,其次,我們再來寫兩個文件,一個爲csdn_en_US.properties,另一個爲: csdn_zh_CN.properties,從這兩個文件中,我們可以看到,他們是csdn這個開頭的,en爲語言,US是國家名字,總結來說是:

開始前綴_語言_國家.properties這樣組成的,因爲是全局的,所以我們放在src的根目錄下, csdn_en_US.properties的內容爲welcome=welcom{0} to beijing{1}\!  另一個csdn_zh_CN.properties,內容爲:

我們再來寫一個jsp的頁面其內容爲

[html] view plain copy
  1. <%-tags" prefix="s"%>  
  2. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  3. <%@ taglib uri="/struts String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.   <head>  
  9.     <base href="<%=basePath%>">  
  10.     <title>My JSP 'index.jsp' starting page</title>  
  11.        <meta http-equiv="pragma" content="no-cache">  
  12.        <meta http-equiv="cache-control" content="no-cache">  
  13.        <meta http-equiv="expires" content="0">      
  14.        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  15.        <meta http-equiv="description" content="This is my page">  
  16.   </head>  
  17.   <body>  
  18.               <s:text name="welcome">  
  19.                      <s:param></s:param>  
  20.                      <s:param>中關村</s:param>  
  21.               </s:text>  
  22. </body>  
  23. </html>  

運行的結果爲

如果你把瀏覽器改成應爲的那麼運行結果爲

這就是全局的前臺頁面的國際化,

我們再來看標籤的國際化,

標籤的國際化一般用戶錯誤信息的提示,,我們在寫一個例子

在struts2.xml文件中配置瞭如下信息

<constantname="struts.custom.i18n.resources"value="csdn"></constant>

我們還是寫上面的兩個文件一個爲csdn_en_US.properties,另一個爲: csdn_zh_CN.properties,

在第一個文件中添加的內容爲:    

第二個爲:

我們在寫一個頁面

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2.   
  3. <%@ taglib uri="/struts-tags" prefix="s"%>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9.   
  10. <html>  
  11.   <head>  
  12.     <base href="<%=basePath%>">  
  13.     <title>My JSP 'index.jsp' starting page</title>  
  14.    <meta http-equiv="pragma" content="no-cache">  
  15.    <meta http-equiv="cache-control" content="no-cache">  
  16.    <meta http-equiv="expires" content="0">      
  17.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.    <meta http-equiv="description" content="This is my page">  
  19.   </head>  
  20.   <body>  
  21.   <form action="${pageContext.request.contextPath}/csdn/add.action">  
  22.   
  23.   用戶名:<input type="text" name="name"><input type="submit" value="提交">  
  24.   </form>  
  25. </body>  
  26. </html>  


運行結果爲

.

我們在寫一個錯誤的頁面,也就是所如果報錯那麼就跳轉到這個頁面

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2.   
  3. <%@ taglib uri="/struts-tags" prefix="s"%>  
  4. <%  
  5. String path = request.getContextPath();  
  6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  7. %>  
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  9. <html>  
  10.   <head>  
  11.     <base href="<%=basePath%>">  
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.    <meta http-equiv="pragma" content="no-cache">  
  14.    <meta http-equiv="cache-control" content="no-cache">  
  15.    <meta http-equiv="expires" content="0">      
  16.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.    <meta http-equiv="description" content="This is my page">  
  18.    <!-- 
  19.    <link rel="stylesheet" type="text/css" href="styles.css"> 
  20.    -->  
  21.   
  22.   </head>  
  23.   <body>  
  24.   <s:fielderror></s:fielderror>  
  25.   </body>  
  26. </html>  

這個頁面就會把錯誤信息顯示出來

同樣我們創建一個action和一個loginAction-validation.xml文件放在也action一個目錄下,這個一個 校驗器,相信大家已經看過了,

Action中寫入

[java] view plain copy
  1. package cn.csdn.action;  
  2. import com.opensymphony.xwork2.ActionSupport;  
  3. public class loginAction extends ActionSupport{  
  4.    private String name;  
  5.    public String getName() {  
  6.       return name;  
  7.    }  
  8.    public void setName(String name) {  
  9.       this.name = name;  
  10.    }  
  11.    public String add(){  
  12.   
  13.       System.out.println("-------------執行了方法");  
  14.   
  15.       return SUCCESS;  
  16.    }  
  17. }  

我們在loginAction-validation.xml寫入

[html] view plain copy
  1. <!DOCTYPE validators PUBLIC  
  2.       "-//Apache Struts//XWork Validator 1.0.3//EN"  
  3.       "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">  
  4. <validators>  
  5.    <field name="name">  
  6.       <field-validator type="requiredstring">  
  7.          <param name="tirm">true</param>  
  8.          <message key="name"></message>  
  9.       </field-validator>  
  10.       </field>  
  11. </validators>  
  12.   
  13. 好了現在就可以運行了:  

如果是中的界面,我們運行的結果爲

如果是應爲的界面,運行的結果爲

和這樣就是標籤的國際化了

我們最後看這個Action類中的國際化:

我們還是以上面的例子爲例,我們不在使用哪個校驗器.我們把action改爲

[java] view plain copy
  1. package cn.csdn.action;  
  2. import java.util.Date;  
  3. import com.opensymphony.xwork2.ActionSupport;  
  4. public class loginAction extends ActionSupport{  
  5.    private String name;  
  6.    public String getName() {  
  7.       return name;  
  8.    }  
  9.    public void setName(String name) {  
  10.       this.name = name;  
  11.    }  
  12.    @Override  
  13.    public void validate() {  
  14.       if ("".equals(name.trim())) {  
  15.          this.addFieldError("names""names.error");  
  16.       }  
  17.    }  
  18.    public String add(){  
  19.       System.out.println("執行了方法");  
  20.       return SUCCESS;  
  21.    }  
  22. }  

我們在properties這個國際化文件中吧名字分別改成name.error和name.error

我們把頁面改成<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

[html] view plain copy
  1. <%@ taglib uri="/struts-tags" prefix="s"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  7. <html>  
  8.   <head>  
  9.     <base href="<%=basePath%>">  
  10.     <title>My JSP 'index.jsp' starting page</title>  
  11.    <meta http-equiv="pragma" content="no-cache">  
  12.    <meta http-equiv="cache-control" content="no-cache">  
  13.    <meta http-equiv="expires" content="0">      
  14.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  15.    <meta http-equiv="description" content="This is my page">  
  16.    <!-- 
  17.    <link rel="stylesheet" type="text/css" href="styles.css"> 
  18.    -->  
  19.   </head>  
  20.   <body>  
  21. <s:text name="names.error"></s:text>  
  22.   </body>  
  23. </html>  
  24.   
  25.    

我們再來測試一下

如果是中的界面,我們運行的結果爲

如果是應爲的界面,運行的結果爲

和這樣就是action的國際化了

 

上面介紹的都是全局的,我們下面來看package這個級別的

在java的包下放置package_language_country.properties資源文件,package爲固定寫法,處於該包及子包下的action都可以訪問該資源。當查找指定key的消息時,系統會先從package資源文件查找,當找不到對應的key時,纔會從常量struts.custom.i18n.resources指定的資源文件中尋找。

我們以第一個例子爲例,但是這個packge的國際化必須由一個action跳轉到一個頁面,才能顯示出來,而不能想全局的那樣直接就能顯示出來,

       所以我們跳轉到error.jsp的時候顯示出來

配置該包的文件:

Package_en_US.properties

Package_zh_CN.properties

我們看一下error.jsp頁面

[html] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2.   
  3. <%@ taglib uri="/struts-tags" prefix="s"%>  
  4.   
  5. <%  
  6. String path = request.getContextPath();  
  7.   
  8. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  9. %>  
  10.   
  11. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  12.   
  13. <html>  
  14.   
  15.   <head>  
  16.   
  17.     <base href="<%=basePath%>">  
  18.   
  19.     <title>My JSP 'index.jsp' starting page</title>  
  20.   
  21.    <meta http-equiv="pragma" content="no-cache">  
  22.   
  23.    <meta http-equiv="cache-control" content="no-cache">  
  24.   
  25.    <meta http-equiv="expires" content="0">      
  26.   
  27.    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  28.   
  29.    <meta http-equiv="description" content="This is my page">  
  30.   
  31.    <!-- 
  32.  
  33.    <link rel="stylesheet" type="text/css" href="styles.css"> 
  34.  
  35.    -->  
  36.   
  37.   </head>  
  38.   
  39.   <body>  
  40.   <s:text name="welcome">  
  41.       </s:text>  
  42.   
  43.   </body>  
  44.   
  45. </html>  
  46.   
  47.    

在測試一下就是顯示了一下這個welcome這個文件.我們看一下運行的結果

  

這就是package的國際化

我們在來看一下這個action的國際化

如這個包的結構,我們必須在action的目錄下創建這個ActionClassName_國家_語言.properties這樣的文件,

和上面的例子一樣爲了區分我們改變了這個porperties這兩個文件

我們再來運行一下項目

看結果

  

 

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