Struts1 x的validator框架使用

2021-08-31 05:48:08 字數 1545 閱讀 6165

一、配置

在使用validator校驗框架時候先要對工程進行配置,新增validator-rules.xml和validator.xml兩個檔案,將其放到web-inf/目錄下(與web.xml檔案在同一目錄下)

在struts-config.xml中設定外掛程式:

value值用來指定驗證規則的檔案,多個驗證規則檔案要用逗號分割。

配置驗證規則:

validator框架有兩個重要的配置檔案:

<1>validation-rules.xml檔案,

這個配置檔案包好了一組可**用程式使用的全域性驗證規則。

這個檔案是所有應用程式都使用的,任何struts應用程式也都能

使用。除非打算修改或擴充套件這組規則,否則無需修改這個檔案!

errors.byte= must be a byte.

errors.short= must be a short.

errors.integer= must be an integer.

errors.long= must be a long.

errors.float= must be a float.

errors.double= must be a double.

errors.date= is not a date.

errors.range= is not in the range through .

errors.creditcard= is an invalid credit card number.

errors.email= is an invalid e-mail address.

## errors

error.username.required=your id is wrong.

error.password.required=your password is wrong.

errors.header=

errors.footer=

因為,驗證出錯的時候,系統會自動在資源包中查詢對應的錯誤訊息,

比如:required驗證規則出現錯誤時,就會輸出「errors.required」的值。

<2>validation.xml檔案

這個檔案是專門用來驗證頁面指定的field。

1)根元素:

validator.xml檔案的「根元素(root)」是form-validation,

意味著整個檔案的內容包含在「

二、使用

一旦你已經配置了驗證器外掛程式,這樣它可以載入你的validator資源,你只需要延長org.apache.struts.validator.action.validatorform而不是org.apache.struts.action.actionform。然後當validate方法被呼叫,操作的從struts配置name屬性用於裝載當前窗體的驗證。表單元素的名稱在確認配置屬性應與動作元素的名稱屬性。

例如:struts-config.xml檔案中某個action的配置

validation.xml檔案中配置:

詳細配置資訊裡面有

struts1 x 學習筆記

struts1.x 工作流程 2.建立actionform物件,儲存表單引數 3.使用actionform的validate方法進行引數驗證 4.actionservlet傳遞請求給相應的action 5.action的execute方法返回相應的actionforward物件 6.actionse...

struts 1 x 學習 筆記1

配置struts 1.x 要做的以下幾件事,1.匯入jar 2.web.xml配置actionservlet 3.struts conf.xml 4.配置formbean 5.配置action path 必須 type 必須 name 是frombean的名字 scope 作用域,只有session...

Struts 1 x的執行緒安全

回到首頁 先簡單的回顧,然後按需,第二輪,第三輪完善。執行緒安全是每個多執行緒程式都要考慮的問題,struts 1.x 也不理額外。如果處理不當就會出現問題,並且很難排查,設計階段必須要留意執行緒安全。軟體設計應該優於開發,做hw系統最大的痛苦就是倒置,因為都是政治任務,每個人都只關注自己的或者自己...