validation.xml的配置

來源:互聯網
上載者:User

 validation.xml的配置

validation.xml檔案是Validator架構需要的另一個設定檔。該檔案是特定於應用程式的,由開發人員自己來建立。它描述了那些具體應用中的ActionForm所使用的validator-rules.xml檔案中的有效性驗證規則。通過ActionForm在validation.xml檔案中配置所需要的驗證規則,開發人員就不必將驗證邏輯寫入程式碼放在ActionForm的內部。一個簡單的validation.xml檔案如常式5-7所示。

常式5-7  validation.xml執行個體

 

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE form-validation PUBLIC"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN""http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">

<form-validation><formset><form name="loginActionForm"><field property="username"depends="required"><arg0 key="prompt.username"/></field><field property="password"depends="required, minlength,maxlength"><arg0 key="prompt.password"/><arg1 key="${var:minlength}" name="minlength"resource="false"/><arg2 key="${var:maxlength}" name="maxlength"resource="false"/><var><var-name>maxlength</var-name><var-value>16</var-value></var><var><var-name>minlength</var-name><var-value>3</var-value></var></field></form></formset></form-validation>

validation.xml檔案中包含form-validation、global、formset、form、field、msg、arg、var等元素。下面對它們一一進行介紹。
1.form-validation元素

<form-validation>元素是validation.xml檔案的根項目。它包含2個子項目,即<global>元素和<formset>元素,其在dtd中的定義如下:

<!ELEMENT form-validation (global*, formset*)>

2.global元素

global元素允許開發人員配置可以用在檔案其他部分中的constant元素,也就是全域常量。這與在Java檔案中定義一個常量,然後在類中使用該常量的方式相似。constant元素以名字和值的方式出現。例如:

<form-validation><global><constant><constant-name>zip</constant-name><constant-value>^\d{5}\d*$</constant-value> <!--常量值,這裡是一個Regex--></constant><global>
<formset><form name="registerForm"><field property ="zipPostal" depends="required, mask"><arg0 key="registerForm.zippostal.displayname"/><var><var-name>mask</var-name><var-value>${zip}</var-value></var></field></form></formset><form-validation>

在以上代碼中,在<global>元素中定義了一個常量zip,在<formset>元素中可以通過${constant-name}的形式,如${zip},來引用這個常量。

3.formset元素

<formset>元素包含兩個子項目,即<constant>元素和<form>元素。<constant>元素可以出現零次或多次,<form>元素至少出現一次。這裡的<constant>元素中定義的constant常量是局部常量,只能在當前<formset>元素內引用。

<formset>元素有2個屬性,即language和country,它們用於國際化。

4.form元素

form元素定義了一套將要進行有效性驗證的域(field),其name屬性指定對應表單的名字,並且name屬性與Struts設定檔中的form-bean元素的name屬性一致。

form元素可以包含多個field元素。

5.field元素

一個field元素對應於一個表單中需要驗證的欄位。它的屬性如表5-3所示。

表5-3  field元素的屬性

屬    性

描    述

property

將要進行有效性驗證的ActionForm的屬性名稱

depends

指定欄位的驗證規則,多個規則之間以逗號分開

page

如果對應的ActionForm是一個跨頁表單,可能包括一個page屬性與表單中的page屬性相對應,使用者指定該欄位應該在哪一頁被處理

indexedListProperty

後迴圈列表,執行該域的有效性驗證

6.msg元素

field元素的msg子項目指定驗證規則對應的訊息文本。該訊息文本將替代預設的訊息文本,即validator-rules.xml中定義的訊息文本。msg元素的值必須是應用程式訊息資源套件中的某個訊息資源的關鍵字。例如:

<field property="lastName" depends="required, mask" page="1"><msg name="mask" key="registerForm.lastname.maskmsg"/><arg0 key="registerForm.lastname.displayname"/><var><var-name>mask</var-name><var-value>^[a-zA-Z]*$</var-value></var></field>

當lastName欄位有效性驗證失敗時,顯示的錯誤訊息是訊息資源中registerForm. lastname.masking鍵所指的訊息文本。

msg元素的屬性如表5-4所示。

表5-4  msg元素的屬性

屬    性

描    述

key

指定綁定的訊息資源中的訊息文本或直接指定訊息文本

name

指定驗證規則的名字

bundle

指定綁定的訊息資源的名稱

resource

當該屬性值為true時,表示使用來自訊息資源的訊息文本;當該屬性值為false時,表示直接在key屬性中設定訊息文本。預設值為true。

7.arg元素

arg元素可以用來向訊息文本中傳遞參數。它有name、key、resource和position等幾個屬性,name、key、resource屬性的含義與msg元素的屬性相當。position屬性用來指定訊息文本中的替換位置。

例如,常式5-8是關於minlength驗證規則的設定檔。

常式5-8  minlength驗證規則的設定檔

<field property="password"depends="required, minlength"><arg0 key="prompt.password" /><arg1 key="${var:minlength}" name="minlength"resource="false" position="1"/><var><var-name>minlength</var-name><var-value>3</var-value></var></field>

在validator-rules.xml檔案中,minlength驗證規則的預設訊息文本是:

{0} can not be less than {1} characters.

在常式5-8中,position屬性的值為1的arg元素定義中,name屬性的值為minlength,表示僅適用於minlength驗證規則,resource屬性的值為false,表示可直接在key屬性中設定訊息文本,即key屬性的值為${var:minlength},在這裡是3。把以上代碼中兩個arg的key值分別放入minlength驗證規則的預設訊息文本中的相應位置,最後返回的訊息文本應該是:

 password can not be less than 3 characters.

8.var元素

var元素用來向驗證規則傳遞參數。例如在常式5-7中,定義了maxlength和minlength兩個參數,分別傳遞給maxlength和minlength驗證規則。

arg元素也可以訪問var元素,文法形式是${var:var-name}。例如,在常式5-7中的${var:maxlength}和${var:minlength}的使用。

 

 

 

9.<html:messages>
Action 中 :
        ActionMessages message = new ActionMessages();
        message.add(" 訊息控制代碼 ",new ActionMessage(" 資源檔中 Key 值 ",String 類型描述資訊 ));
        this.addMessages(request,message);
        return ActionForward;
JSP 頁面中 :
        <html:messages id=" 指定使用訊息的標識 " property=" 訊息控制代碼 " message="true|false">
           <bean:write name=" 以上所指 ID 標識 "/>
        </html:messages>

例如:

Action方法中:
saveMessage(request,"頁面要顯示的訊息");

Action的調用的方法,可以寫真Action實作類別的父類 BaseDispatchAction中:

public ActionMessages saveMessage(HttpServletRequest request, String key) {
      ActionMessages messages = new ActionMessages();
      return saveMessage(messages, request, key);
}

protected ActionMessages saveMessage(ActionMessages messages,
       HttpServletRequest request, String key) {

      messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
      saveMessages(request, messages);
      return messages;
}

頁面:

<html:messages id="msg" message="true"><bean:write name="msg"/></html:messages>

 

<html:errors>
Action 中 :
        ActionMessages message = new ActionMessages();
        message.add(" 訊息控制代碼 ",new ActionMessage(" 資源檔中 Key 值 ",String 類型描述資訊 ));
        this.saveErrors(request,message);
        return ActionForward;
JSP 頁面中 :
        <html:errors property=" 訊息控制代碼 "/>

如果Action中這樣設定(false),頁面的提示資訊將不從資源檔裡讀取:
        ActionMessages message = new ActionMessages();
        message.add("訊息控制代碼",new ActionMessage("String類型描述資訊));",false));
        this.saveErrors(request,message);
        return ActionForward;
JSP頁面:
        <html:errors/>或<html:errors property="訊息控制代碼"/>

 10.

關於message-resources 配置中parameter的值   

³parameter的值,可以指定資源檔的位置和名稱³舉例:³<message-resources parameter="MessageResources" />³表示在類路徑根目錄(WEB-INF/classes目錄)下有MessageResources_XX_XX.properties檔案(注意:國家代碼可以省略,跟java中對資源屬性檔案的處理一樣)³<message-resources parameter="resources.application"/>³表示在類路徑根目錄下,有一個resources目錄,在這個resources目錄中存放著所有的application_XX_XX.properties資源屬性檔案 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.