struts2 驗證 筆記

來源:互聯網
上載者:User

struts2驗證分局部驗證和全域驗證,今天複習的內容為:

1.局部驗證

        對某個具體的Action做細緻化驗證,那麼在此action所在目錄下建立一個同名稱的properties檔案,如對LoginAction.java進行局部驗證,則建立一個LoginAction.properties檔案,檔案內容形如:

         invalid.fieldvalue.username=userName error

        中文需轉成unicode編碼,(可調用jdk中內建的native2ascii工具進行轉換). 注意:invalid.fieldvalue 不要寫錯

2. 全域驗證

     全域驗證可以對整個工程中進行匹配驗證,做如下操作來實現:

     1.struts.xml中間中配置全域變數

             <constant name="struts.custom.i18n.resources" value="message"></constant>

          注意:struts.custom.i18n.resources 不要寫錯,固定寫法

      2.在struts.xml同級目錄下建立 message.properties 檔案,檔案內容為:

          xwork.default.invalid.fieldvalue={0} error

          注意:xwork.default.invalid.fieldvalue 不要寫錯

注意:在擷取了用戶端請求後,struts2會對錶單資料先進行局部驗證,若無局部驗證則進行全域驗證,二者皆無則驗證。 表單發送到servlet後是先進行轉換再進行校正。:)

聯繫我們

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