Struts2國際化介紹及詳細說明
我們來了解一下這個國際化:國際化是在軟件設計和文檔開發過程中,使得功能和代碼設計能處理多種語言和文化習俗,在創建不同語言版本時,不需要重新設計源程序代碼的軟件工程方法。
我們看struts2的國際化,首先在struts2的國際化,我們應必備的前提,首先如果我們要使用struts2的國際化,我們首先要在struts2中配置一個標籤如:<constantname="struts.custom.i18n.resources"value="csdn"></constant>,這樣我們就能使用struts2中的國際化了,但是這個配置必須的是你在你的struts2配置環境能用的基礎上.
我們在看具體的使用:
struts2的國際化分三種情況,同時也分三個範圍,
1.前臺頁面的國際化,
2.Action類中的國際化,
3.配置文件的國際化。
我們首先來看全局的國際化,
1>前臺國際化:
我們首先配置一下struts.xml文件中的配置,我們寫一個例子:
如:
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
- "http://struts.apache.org/dtds/struts-2.3.dtd">
- <struts>
- <constant name="struts.custom.i18n.resources" value="csdn"></constant>
- <include file="struts-validate.xml"></include>
- <package name="a" extends="struts-default" namespace="/">
- </package>
- </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的頁面其內容爲
- <%-tags" prefix="s"%>
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="/struts String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- <s:text name="welcome">
- <s:param>你</s:param>
- <s:param>中關村</s:param>
- </s:text>
- </body>
- </html>
運行的結果爲
如果你把瀏覽器改成應爲的那麼運行結果爲
這就是全局的前臺頁面的國際化,
我們再來看標籤的國際化,
標籤的國際化一般用戶錯誤信息的提示,,我們在寫一個例子
在struts2.xml文件中配置瞭如下信息
<constantname="struts.custom.i18n.resources"value="csdn"></constant>
我們還是寫上面的兩個文件一個爲csdn_en_US.properties,另一個爲: csdn_zh_CN.properties,
在第一個文件中添加的內容爲:
第二個爲:
我們在寫一個頁面
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="/struts-tags" prefix="s"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- </head>
- <body>
- <form action="${pageContext.request.contextPath}/csdn/add.action">
- 用戶名:<input type="text" name="name"><input type="submit" value="提交">
- </form>
- </body>
- </html>
運行結果爲
.
我們在寫一個錯誤的頁面,也就是所如果報錯那麼就跳轉到這個頁面
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="/struts-tags" prefix="s"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <s:fielderror></s:fielderror>
- </body>
- </html>
這個頁面就會把錯誤信息顯示出來
同樣我們創建一個action和一個loginAction-validation.xml文件放在也action一個目錄下,這個一個 校驗器,相信大家已經看過了,
Action中寫入
- package cn.csdn.action;
- import com.opensymphony.xwork2.ActionSupport;
- public class loginAction extends ActionSupport{
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String add(){
- System.out.println("-------------執行了方法");
- return SUCCESS;
- }
- }
我們在loginAction-validation.xml寫入
- <!DOCTYPE validators PUBLIC
- "-//Apache Struts//XWork Validator 1.0.3//EN"
- "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
- <validators>
- <field name="name">
- <field-validator type="requiredstring">
- <param name="tirm">true</param>
- <message key="name"></message>
- </field-validator>
- </field>
- </validators>
- 好了現在就可以運行了:
如果是中的界面,我們運行的結果爲
如果是應爲的界面,運行的結果爲
和這樣就是標籤的國際化了
我們最後看這個Action類中的國際化:
我們還是以上面的例子爲例,我們不在使用哪個校驗器.我們把action改爲
- package cn.csdn.action;
- import java.util.Date;
- import com.opensymphony.xwork2.ActionSupport;
- public class loginAction extends ActionSupport{
- private String name;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public void validate() {
- if ("".equals(name.trim())) {
- this.addFieldError("names", "names.error");
- }
- }
- public String add(){
- System.out.println("執行了方法");
- return SUCCESS;
- }
- }
我們在properties這個國際化文件中吧名字分別改成name.error和name.error
我們把頁面改成<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="/struts-tags" prefix="s"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <s:text name="names.error"></s:text>
- </body>
- </html>
我們再來測試一下
如果是中的界面,我們運行的結果爲
如果是應爲的界面,運行的結果爲
和這樣就是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頁面
- <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
- <%@ taglib uri="/struts-tags" prefix="s"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <title>My JSP 'index.jsp' starting page</title>
- <meta http-equiv="pragma" content="no-cache">
- <meta http-equiv="cache-control" content="no-cache">
- <meta http-equiv="expires" content="0">
- <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
- <meta http-equiv="description" content="This is my page">
- <!--
- <link rel="stylesheet" type="text/css" href="styles.css">
- -->
- </head>
- <body>
- <s:text name="welcome">
- </s:text>
- </body>
- </html>
在測試一下就是顯示了一下這個welcome這個文件.我們看一下運行的結果
這就是package的國際化
我們在來看一下這個action的國際化
如這個包的結構,我們必須在action的目錄下創建這個ActionClassName_國家_語言.properties這樣的文件,
和上面的例子一樣爲了區分我們改變了這個porperties這兩個文件
我們再來運行一下項目
看結果