FormBean的驗證:
1、重寫FormBean的validate方法。
public ActionErrors validate(ActionMapping mapping,
2、在struts-config.xml中配置action節點的validate與input屬性,validate表示是否調formBean的validate方法,input是如果出錯的話跳轉的錯誤處理界面。注意:如果formBean重寫的了validate方法并且action節點validate屬性為true,那一定要添加input屬性。
3、在struts-config.xml中添加<message-resources parameter="org.koyo.views.login" />注意:其中的parameter屬性的值是login.properties文件的路徑。(容易被漏掉,多加注意)
4、在界面使用<html:errors />或<html:errors property="nameEmpty"/>顯示錯誤信息。
步驟2、3、4,尤其是步驟3,經常會漏掉,應多加注意?。?!
ActionBean的驗證
1、在ActionBean中添加代碼。
ActionMessages messages = new ActionMessages();//不要與FormBean混淆,此處直接就是ActionMessage
步驟2、3、4,與FormBean驗證相同。
主要區別:ActionBean驗證主要做業務,需要與數據庫交互,比如用戶名是否可用,轉賬余額是否可用,出貨余額等驗證;formBean雖然完成客戶端驗證功能,但需要與服務器交互。注意不同的區分標準。。
本文章大部分內容轉載自 西安云工廠http://www.xaygc.com/struts.html