struts2 學習(七) ----- 輸入校正

來源:互聯網
上載者:User

標籤:jsp   struts2   

struts2輸入校正(伺服器端驗證)

一、使用validate方法進行輸入校正

步驟:1) 重寫Action的validate()函數

      2) 在struts2.xml中配置Action的別名為”input”的result

      3) 在輸出頁面上顯示錯誤:使用<s:fielderror>標籤


1. 建立LoginAction.java的時候要繼承ActionSupport類:


2. 配置:


3.使用<s:fielderror>標籤




二、使用struts2校正架構實現輸入校正

 

步驟:1) 配置攔截器

      2) 編寫校正的設定檔

         a)檔案名稱格式必須為:Action類的名字-validation.xml

         b)校正設定檔必須和Action類放在同一目錄

      3) 在輸入頁面上顯示錯誤:使用<s:fielderror>標籤


1.去掉Action的validate()函數


在stuts.xml中配置


2.編寫校正的設定檔


3.使用<s:fielderror>標籤



三、自訂校正器

步驟:1) 建立一個校正器類,繼承FieldValidatorSopport父類,實現validate()函數

            2) 註冊校正器

                  a)設定檔的檔案名稱必須為”validators.xml”,必須放在src根目錄下

                  b)校正器配置的思路:給校正器一個別名

           3) 測試:使用該校正器


1.建立一個class,名為UserNameValidator.java , 繼承FieldValidatorSopport.代碼如下:


2.註冊校正器



3.測試


struts2 學習(七) ----- 輸入校正

聯繫我們

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