1. 程式人生 > >Struts學習之Message Resources配置詳解

Struts學習之Message Resources配置詳解

目錄

一、概述

二、用法

三、建立資源包

四、配置

五、資原始檔放在哪裡

六、Tags

七、Actions

八、國際化

九、JSTL

十、結論


一、概述


       Message Resources訊息資源是Strus框架定義的一組訊息資源包,用來簡化開發、支援國際化的,開發者可以自由的將頁面Title,Label Name,Error Message等顯示資訊定義在不同的資原始檔中,然後在jsp、java中呼叫並顯示,在需要更改語言時,只要更改應用的配置就可以實現呼叫不通的資原始檔從而實現國際化。
        類MessageResources可以使開發者方便地支援多語言,包括支援多時間格式和數字格式。使用資源包的另一個好處是允許開發者將標籤字串集中儲存在一個位置,而不是分散在不同的JSP頁面裡。例如,對於每個使用者的名字的標籤"First Name" ,我們可以將它寫在資源包中,在適當的地方通過Struts標籤簡單的進行引用:
<bean:write key="label.first.name"/>
這樣做將會讓你對程式的更改變的簡單容易,你不必在每一個JSP頁面裡更改標籤的內容了。


二、用法


使用訊息資源包需要你做下面的事情:
1. 為你想要支援的地方建立一個訊息資源包。
2. 配置WEB應用,載入訊息資源包。
3. 使用相應的JSP標籤載入資源或者...
4. ...在一個Action類中載入資源。


三、建立資源包


MessageResources 類的預設的實現是一個包含"key=value" 對的檔案,下面的一個訊息資源包檔案的例子。
label.username=Username
l
label.password=Password
l
label.first.name=First Name
l
label.last.name=Last Name
l
label.email=Email Address
l
label.phone.number=Phone Number
l
label.welcome=Welcome back {0} {1}!
e
error.min.length=The input must be at least {0} characters in length.
e
error.max.length=The input cannot be longer than {0} characters in length.
大括號包圍的整數是對java.text.MessageFormat 類的支援,程式設計師可以向value字串中傳遞引數,對每個value字串你最多可以傳遞4個引數。


四、配置


有兩種途徑通知Struts你的資源包的位置:web.xml 檔案或者struts-config.xml 檔案。首先來看web.xml 檔案的配置:
<servlet>
<
<servlet-name>action</servlet-name>
<
<servlet-class>
      org.apache.struts.action.ActionServlet
<
</servlet-class>
<
<init-param>
<
<param-name>
      application
<
</param-name>
<
<param-value>
      com.systemmobile.example.ApplicationResources
<
</param-value>

</init-param>
<
</servlet>
這個配置說明你的資源包的名字是ApplicationResources.properties,它位於com.systemmobile.example 包中。字尾".properties" 是隱含的,你不必顯式地寫出來。如果你還有另一個資原始檔在相同的包中,例如ApplicationResources_fr.properties ,用來支援法語,你只需要象上面定義的那樣列出檔名字即可。
定義資原始檔的第二中方法(上面已經提到),是在struts-config.xml 檔案中配置:
<message-resources parameter="com.systemmobile.example.ApplicationResources"/>
屬性parameter 是必須的。和在web.xml檔案中配置一樣, 需要注意的是檔案在包中的位置。
使用struts-config.xml 檔案來配置訊息資原始檔是推薦的做法,因為它更有可擴充套件性,更靈活。
l    你可以使用message-resources 標籤從不同的資原始檔取不同的訊息,前提是在配置的時候為不同的資原始檔給出不同的key 屬性的值。例如:
<message-resources key="myResources" parameter="com.systemmobile.example.
A
ApplicationResources"/>
<
<message-resources key="moreResources" parameter="com.systemmobile.example.
M
MoreApplicationResources"/>
然後你必須這樣使用bean:message 標籤:
<bean:message bundle="moreResources" key="some.message.key"/>
l    設定屬性null 的值為"false" 後,如果某個資源字串不存在將返回???key??? 而不是僅僅顯示null。這樣很容易在JSP頁面中看到你沒有定義的資源,使得內部測試的速度更快。(關於如何從資原始檔中獲得訊息的詳細內容參見國際化 一節)
<message-resources parameter="com.systemmobile.example.ApplicationResources" null="false"/>
l    另外,message-resources 標籤允許你使用自己實現的MessageResourcesFactory 介面,這不在本文的討論範圍。

五、資原始檔放在哪裡


關於資原始檔最常見的問題是將資原始檔放在WAR檔案的哪裡。簡單的回答是該檔案必須放在你的classpath下面,這意味著將資原始檔放在一個JAR 檔案中,或者放在/WEB-INF/classes 目錄極其子目錄下。下表給出了資原始檔的位置,在message-resources 標籤中"parameter" 屬性的值以及簡短的說明。
 
Resources Location
parameter Value
Description
/WEB-INF/classes/ApplicationResources.properties
ApplicationResources
檔案放在classes 目錄下, 該目錄在web應用的classpath中.
/WEB-INF/classes/resources/ApplicationResources.properties
resources.ApplicationResources
該檔案放在"resources"目錄下, 所以包名也就是路徑名要給出。
In the app.jar file, in the com.systemmobile.example package/directory.
com.systemmobile.example.ApplicationResources
檔案在JAR檔案中的全路徑。
 
 

六、Tags


最常用Struts 標籤是bean:message 標籤。使用這個標籤的"key" 可以從資原始檔中讀特定的訊息資源。你還可以傳入四個引數中的一個或全部:
<bean:message key="label.password"/>
<bean:message key="error.min.length" arg0="6"/>
<bean:message key="label.welcome" arg0="Ralph" arg1="Nader"/>
html:message 可以讓你向用戶顯示錯誤資訊(預設)或訊息資訊,而html:errors 只顯示錯誤資訊。很明顯,錯誤資訊或訊息資訊一定要儲存在request裡,否則就什麼也不會顯示。這裡有一個顯示訊息資訊的例子:
<logic:messagesPresent message="true">
    <html:messages id="msg" message="true">
      <div class="success">
        <bean:write name="msg"/>
      </div><br/>
    </html:messages>
</logic:messagesPresent>
還有一些標籤也有限地支援訊息資源,比如html:link。html:link標籤通過定義"titleKey" 屬性來顯示標題文字。許多html 使用 "altKey" 屬性從資原始檔裡獲得alternate(替代)文字。


七、Actions


你還可以在Action 類中使用訊息資原始檔。Action 類有兩個方法得到一個MessageResource 類的例項:
// 返回一個request裡的資原始檔
protected MessageResources getResources(HttpServletRequest request);
// 返回一個request裡的資原始檔,
// 該資原始檔的標誌上<message-resources/> 元素的內容
protected MessageResources getResources(javax.servlet.http.HttpServletRequest request,
 java.lang.String key);
MessageResources類可以讓你從資原始檔中得到本地化的訊息。The API for MessageResources 可以在資源中找到。比較常用的方法有:
// these methods load a resources key for the given locale
public String getMessage(java.util.Locale locale, java.lang.String key);
public String getMessage(java.util.Locale locale, java.lang.String key, 
         java.lang.Object arg0);
public String getMessage(java.util.Locale locale, java.lang.String key, 
         java.lang.Object[] args);
public String getMessage(java.util.Locale locale, java.lang.String key, 
         java.lang.Object arg0, java.lang.Object arg1)
public String getMessage(java.util.Locale locale, java.lang.String key, 
         java.lang.Object arg0, java.lang.Object arg1, java.lang.Object arg2);
public String getMessage(java.util.Locale locale, java.lang.String key, java.lang.Object arg0,
         java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3);
// these methods load a resources key for the locale retrieved
// from the HttpServletRequest
public String getMessage(java.lang.String key);
public String getMessage(java.lang.String key, java.lang.Object arg0);
public String getMessage(java.lang.String key, java.lang.Object[] args);
public String getMessage(java.lang.String key, java.lang.Object arg0, 
         java.lang.Object arg1);
public String getMessage(java.lang.String key, java.lang.Object arg0, 
         java.lang.Object arg1, java.lang.Object arg2);
public String getMessage(java.lang.String key, java.lang.Object arg0, 
         java.lang.Object arg1, java.lang.Object arg2, java.lang.Object arg3);
這些返回的字串可以被設定成request 或 session 的引數並串會表現層。你可能注意到了一些過載方法getMessage(...) 選擇了引數Object,而另外一些採用了引數arg0...arg3。這和 bean:message arg0...arg3 屬性等價。
除了MessageResources 類,還有一些類使用了資原始檔。ActionMessage類被用來從action 向JSP之間傳遞訊息資源中的keys 。訊息被用來作為bean 的屬性。ActionError, ActionMessage的子類,使用訊息資源中的keys 儲存驗證失敗後的錯誤資訊。

八、國際化


從資原始檔中提取一個本地化資訊可以由類MessageResources 來處理,或者由它的直接子類PropertyMessageResources類處理。既然類PropertyMessageResources 等經常地被用到,那麼我們就來看看它是怎樣使用getMessage(Locale, String) 方法來從資原始檔中讀取訊息的。
舉例說明:
1. 如果你在ApplicationResources_pt_br.properties (Brazilian Portuguese)中沒有發現訊息的定義,系統將在ApplicationResources_pt.properties 檔案中找,如果ApplicationResources_pt.properties 檔案不存在或者也沒有該訊息,那就去ApplicationResources.properties 檔案裡查找了。
2. 如果訊息找到了,它就被加到本地化的快取中並返回java.lang.String型資料。
3. 如果訊息沒有找到,此時如果returnNull 屬性被為預設的true,將返回 null。 否則將返回類似 ???key??? 的字串,key 就是那個被傳遞的引數。


九、JSTL


JSTL (JavaServer Pages Standard Tag Library) 的fmt標籤最近開始流行起來,用來向JSP中顯示資原始檔的資訊。它能很好地和Struts結合在一起。使用它非常簡單,只要下載JSTL 的jar 檔案和TLDs 並把它們拷貝到你的應用的正確的位置,然後在web.xml檔案中加入下面的內容:
<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>ApplicationResources</param-value>
</context-param>
上面的配置是假定你的ApplicationResources.properties檔案是放在/WEB-INF/classes 目錄下的。 參見above 更多情況。
然後將這個標籤庫直接放在你的JSP中:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
最後,下面的標籤將顯示資原始檔的內容:
<fmt:message key="label.first.name"/>
還有一個使用fmt 標籤獲得資源的例子。(注意: 該段程式取自Jakarta JSTL examples。)
// loading a resource from a specific bundle and populating a parameter
<fmt:message key="currentTime" bundle="${deBundle}">
 <fmt:param value="${currentDateString}"/>
</fmt:message>
// using the forEach iterator to populate paramters
<fmt:message key="serverInfo" bundle="${deBundle}">
 <c:forEach var="arg" items="${serverInfoArgs}">
    <fmt:param value="${arg}"/>
 </c:forEach>
</fmt:message>


十、結論


在向JSP檔案方便地傳遞訊息的同時,Struts使用訊息資原始檔還幫助我們建立國際化的Web應用。我們既可以使用正在快速發展中的JSTL標籤,也可以使用Struts標籤,或直接在action中得到一條具體的訊息。我希望這篇文章為您闡明瞭一些Struts中常用的但有時會混淆的東西。