首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

WebWork 2 源碼深度解析 (三):驗證攔截器與客戶端/伺服端雙重驗證架構

     編輯:Java 相關知識

前言

WebWork 2 (Struts 2 前身) 採用攔截器鏈架構,資料驗證同樣由攔截器實現。本文分析伺服端 ValidationInterceptor 與客戶端腳本驗證體系。

一、伺服端驗證核心流程

1. ValidationInterceptor

public class ValidationInterceptor extends AroundInterceptor {
    protected void before(ActionInvocation invocation) throws Exception {
        Action action = invocation.getAction();
        String context = invocation.getProxy().getActionName();
        ActionValidatorManager.validate(action, context); // 核心委派
    }
    protected void after(ActionInvocation dispatcher, String result) {}
}

2. ActionValidatorManager — 驗證引擎

  • 快取機制:validatorCache (Class+Context → Validator 清單)、validatorFileCache (驗證 XML 檔案時間戳)。
  • 驗證流程:
    1. 根據 Action 類別與 Context (Action 名稱) 解析 ActionName-validation.xml。
    2. 建立 DelegatingValidatorContext (包裝 Action、提供錯誤訊息國際化、屬性存取)。
    3. 遍歷 Validator 清單,逐一呼叫 validator.validate(action)。
    4. 驗證失敗將錯誤加入 ValidatorContext,最終拋出 ValidationException 或由 Result 處理。
public static void validate(Object action, String context) throws ValidationException {
    ValidatorContext ctx = new DelegatingValidatorContext(action);
    validate(action, context, ctx);
}

public static void validate(Object action, String context, ValidatorContext ctx) throws ValidationException {
    List validators = getValidators(action.getClass(), context); // 解析 XML + 快取
    for (Validator v : validators) {
        v.setValidatorContext(ctx);
        v.validate(action); // 核心驗證邏輯
    }
}

3. 驗證 XML 範例 (UserAction-validation.xml)

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN"
       "https://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
<validators>
  <field name="email">
    <field-validator type="requiredstring">
      <message>Email 不能為空</message>
    </field-validator>
    <field-validator type="email">
      <message>Email 格式錯誤</message>
    </field-validator>
  </field>
  <field name="age">
    <field-validator type="int">
      <param name="min">18</param>
      <param name="max">100</param>
      <message>年齡需在 18-100 之間</message>
    </field-validator>
  </field>
</validators>

二、客戶端驗證架構

  • 套件:com.opensymphony.webwork.validators。
  • 核心介面:ScriptValidationAware — 驗證器需實作以產出 JavaScript 程式碼片段。
  • 流程:JSP 標籤 (<ww:form>) 遍歷欄位驗證器 → 呼叫 getValidationScript() → 輸出內嵌 JS → 表單提交前瀏覽器端執行。
  • 優點:即時回饋、減輕伺服器負載;缺點:可被繞過,**伺服端驗證為必須**。

三、自訂驗證器開發步驟

  1. 實作 Validator 介面 (或繼承 FieldValidatorSupport)。
  2. 在 validators.xml (XWork 核心配置) 註冊 <validator name="customType" class="com.xxx.CustomValidator"/>。
  3. 驗證 XML 中使用 <field-validator type="customType">。
  4. 若需客戶端支援,額外實作 ScriptValidationAware 並註冊至 WebWork validator 配置。

四、最佳實踐總結

  1. 分層驗證:客戶端負責 UX (即時提示),伺服端負責安全與資料完整性。
  2. 訊息國際化:驗證訊息鍵值化,透過 DelegatingValidatorContext 結合 TextProvider 解析。
  3. 短路優化:short-circuit 屬性可在欄位首個驗證失敗時跳過後續驗證。
  4. 避免業務邏輯滲入驗證器:驗證器僅檢查格式、範圍、必填;業務規則 (如「郵箱已註冊」) 放在 Action 或 Service 層。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved