六、Struts2之輸入校正

來源:互聯網
上載者:User
文章目錄
  •  
  • 2.資料校正檔案內容
  • 用戶端校正方法:
  • 短路校正器
  • 短路校正器的規則:
  • 校正器的運行順序

 

一、Struts2輸入校正介紹

 

Struts2的輸入校正和類型轉換都是對請求參數進行處理。

輸入校正顧名思義就是請求參數是否能夠滿足一定的要求;

 

用戶端校正&伺服器端校正

用戶端校正是指在瀏覽器這端通過Javascript進行初步校正,為了減輕伺服器端的負載;

伺服器端校正是校正資料的最後一道防線;

 

在Struts2中,資料校正不需要寫任何代碼,只需要一個設定檔,配置校正的條件就可以了,非常簡單;因此資料校正檔案是資料校正的最重要的內容;

 

二、資料校正規則檔案

 

1.資料校正規則檔案規則:

 

1.此檔案中規定了參數的一些校正條件;

2.可以包括欄位型校正器和非欄位型校正器;

3.命名規則:ActionName-validation.xml,其中ActionName是Action類的名字;

4.每個Action都有一個validation檔案,規則檔案放在和Action同目錄下;

5.在前面我們講到的配置邏輯action中,如果需要對某個邏輯Action配置資料校正檔案,則命名規則為:ActionName-LogicActionName-validation.xml;

6.注意:當需要對邏輯action進行校正時,如果存在一般形式的ActionName-validaton.xml檔案,則也會對此邏輯Action進行校正;

7.輸入校正失敗後,和類型轉換一樣,會將錯誤封裝成fieldError,並放入Action Context中,因此在JSP中添加<s:fielderror/>可顯示錯誤,此內容也是我們在資料校正檔案中配置的;

8.當輸入校正失敗後,和類型轉換一樣,返回邏輯視圖為input,因此在struts.xml中必須配<result name="input"></input>

9.輸入校正失敗後,struts表單標籤也會自動輸出錯誤提示;

 

欄位型校正器&非欄位型校正器

 

欄位型校正器:以每個Action屬性為一個單位進行編寫,即以如下風格編寫:

屬性1

        規則1

        規則2

屬性2

        規則1

        規則2

 

 

非欄位型校正器:以規則為一個單位,以如下風格編寫:

規則1

        屬性

規則2

        屬性

規則3

        屬性

 2.資料校正檔案內容

 

1.資料校正檔案以<validators>為根項目;

2.欄位型校正器是以屬性為單位的,內容範本:

 

<validators><field name=""><field-validator type=""><param name=""></param><message></message></field-validator></field></validators>

 

3.非欄位型校正器是以校正器為單位的,內容範本:

 

<validator type=""><param name="fieldName"></param><param name=""></param><message></message></validator>

 

 

程式碼範例:

 

此段代碼中分別對字串、日期、整型、email進行資料校正;

Validation01Action.java

 

package org.validation.action;import java.util.Date;import com.opensymphony.xwork2.ActionSupport;public class Validation01Action extends ActionSupport {private String name;private String email;private int age;private Date date;public String execute()throws Exception{return SUCCESS;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}}

1.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@taglib prefix="s" uri="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <title>My JSP '1.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><s:form action="validator01"><s:textfield name="name" label="姓名"></s:textfield><s:textfield name="email" label="郵箱"></s:textfield><s:textfield name="age" label="年齡"></s:textfield><s:textfield name="date" label="日期"></s:textfield><s:submit value="提交"></s:submit></s:form>  </body></html>

 

如果是以欄位型校正器配置,則參考以下:

Validation01Action-validation.xml

(1)<field name="">為一個屬性,name為Action屬性名稱;

(2)<field-validator type="">為配置一個校正器,type為校正器的類型;

(3)<param>為配置參數,比如最小值、最大值;

(4)<message>為錯誤資訊,如果需要對錯誤提示資訊進行國際化,則可以在全域國際化資源檔中配置,在校正規則檔案中編寫如下形式:<message key="key"/>,key為國際化資源檔的key即可;

 

 

<!DOCTYPE validators PUBLIC        "-//Apache Struts//XWork Validator 1.0.2//EN"        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"><validators><field name="name"><field-validator type="requiredstring"><param name="trim">true</param><message>姓名不可為空</message></field-validator><field-validator type="regex"><param name="expression"><![CDATA[(\w{4,10})]]></param><message>姓名要在4-10位之間</message></field-validator></field><field name="age"><field-validator type="requiredstring"><param name="trim">true</param><message>年齡不可為空</message></field-validator><field-validator type="int"><param name="min">0</param><param name="max">200</param><message>年齡範圍在0-200之間</message></field-validator></field><field name="email"><field-validator type="requiredstring"><param name="trim">true</param><message>郵箱不可為空</message></field-validator><field-validator type="email"><message>郵箱格式不正確</message></field-validator></field><field name="date"><field-validator type="requiredstring"><param name="trim">true</param><message>日期不可為空</message></field-validator><field-validator type="date"><param name="min">2010-01-01</param><param name="max">2010-12-31</param><message>日期在2010-01-01到2010-12-31之間</message></field-validator></field></validators>

而如果採用非欄位校正器,則

(1)<validator type="">表示一個校正器,type屬性工作表示校正器的類型;

(2)<param name="fieldName"></param> 此元素是必要的,指定Action屬性的名稱;

(3)<message>表示錯誤資訊;

<!DOCTYPE validators PUBLIC        "-//Apache Struts//XWork Validator 1.0.2//EN"        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"><validators><validator type="requiredstring" short-circuit="true"><param name="fieldName">name</param><param name="trim">true</param><message key="aa"></message></validator><validator type="requiredstring" short-circuit="true"><param name="fieldName">age</param><param name="trim">true</param><message>年齡不可為空</message></validator><validator type="requiredstring" short-circuit="true"><param name="fieldName">email</param><param name="trim">true</param><message>郵箱不可為空</message></validator><validator type="requiredstring" short-circuit="true"><param name="fieldName">date</param><param name="trim">true</param><message>日期不可為空</message></validator><validator type="regex"><param name="fieldName">name</param><param name="expression"><![CDATA[(\w{4,10})]]></param><message>姓名要在4-10位之間</message></validator><validator type="int"><param name="fieldName">age</param><param name="min">0</param><param name="max">200</param><message>年齡範圍在0-200之間</message></validator><validator type="email"><param name="fieldName">email</param><message>郵箱格式不正確</message></validator><validator type="date"><param name="fieldName">date</param><param name="min">2010-01-01</param><param name="max">2010-12-31</param><message>日期在2010-01-01到2010-12-31之間</message></validator></validators>

 

用戶端校正方法:

 

在JSP的<s:form>元素中添加屬性 validate="true";添加後,在校正規則檔案的規則會轉變為用戶端校正規則,即任何校正都不需要接觸伺服器;

需要注意的是:如果直接存取JSP頁面,並不能體現出用戶端校正,只有通過訪問某個action然後跳轉到JSP頁面才可以讓用戶端校正生效;

 

短路校正器

 

一般來說,如果我們為name配置了兩個校正器:第一個是“不可為空”,第二個是“長度大於4且小於10”,則如果name欄位為空白時,則會同時輸出這兩條提示,但是實際上,如果使用者輸入為空白的情況下,只應該輸出“不可為空”,這就需要短路校正器;

在<validator>或<field-validator>元素中添加short-circut="true"即可;

 

短路校正器的規則:

 

1.當非欄位校正器校正失敗,則其後的所有欄位校正器都不會執行,而不會影響其他非欄位校正器;

2.欄位校正器校正失敗時,其後的所有欄位校正器都不會執行;

 

校正器的運行順序

 

1.非欄位校正器比欄位校正器先執行;

2.從前往後執行;

 

 

三、內建校正器

 

在xwork-core-Xxx.jar的com/opensymphony/xwork2/validator/validators/default.xml中定義了內建的校正器;

以下我們開始介紹內建校正器,以下介紹根據欄位型校正器介紹;非欄位校正器只需要簡單變換即可;

 

1.required校正器

 

此校正器要求欄位必須為非空;

 

2.requiredstring校正器

 

此校正器要求檢驗字串非空並且必須長度大於0,即不能是""

選擇性參數為:

(1)trim:如果為true,則將前後的空白去掉,類似於String的trim函數;

 

3.int校正器

 

此校正器要求整數限定在一定範圍之內;

選擇性參數為:

(1)min:最小值;

(2)max:最大值;

 

4.date校正器

 

要求日期在一定範圍之內;

選擇性參數:

(1)min:最小日期;

(2)max:最大日期;

 

5.fieldexpression校正器

 

要求此校正器滿足指定的邏輯運算式;

選擇性參數:

(1)expression:邏輯運算式;格式為:<![CDATA[(邏輯運算式)]]>

 

6.email校正器

 

要求欄位一定要滿足電子郵箱格式;

 

7.url校正器

 

要求欄位格式滿足URL格式;

 

8.stringlength校正器

 

要求欄位是string,並且長度在一定範圍之內;

選擇性參數:

(1)trim:刪除前後空白;

(2)minLength:最小長度;

(3)maxLength:最大長度;

 

9.regex校正器

 

要求滿足給定的Regex;

選擇性參數:

(1)expression:給出Regex;格式如:<![CDATA[(Regex)]]>

(2)caseSensitive:是否區分大小寫;

 

四、手動校正

 

如果內建校正器不滿足要求,可以自訂校正器,方法是在Action類中重寫public void validate()方法;並且如果不滿足條件,調用addFieldError("name","message"),然後返回input邏輯視圖;

 

當然如果一個Action類中有多個邏輯Action,則validate方法就不管用,因此需要定義validateXxx()方法;比如:

如果邏輯Action的處理方法是fun(),則定義validateFun()方法,實現和validate方法一樣;

 

注意:如果邏輯Action的校正函數是validateXxx(),則validate()一樣也會被調用,因為只要對此物理Action進行校正,則validate()方法總會起作用。這和前面輸入校正規則檔案時一樣的;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.