posts - 15,comments - 0,trackbacks - 0

          FormBean的驗證:

          1、重寫FormBean的validate方法。

          public ActionErrors validate(ActionMapping mapping,  HttpServletRequest request) {
            // TODO Auto-generated method stub
            ActionErrors errors = new ActionErrors();
            if (name == null || name.equals("")) { //nameEmpty為錯誤信息名
             errors.add("nameEmpty", new ActionMessage("empty.error", "UserName"));
            }
            if (pwd == null || pwd.equals("")) {
             errors.add("pwdEmpty", new ActionMessage("empty.error", "PassWord"));
            } //empty.error定義在login.properties文件中,其value值為{0} must not be empty!所以empty.error后面會有UserName和PassWord。
            return errors;
           }
          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
             messages.add("inputError", new ActionMessage("input.error"));
             super.saveErrors(request, messages);//此句容易被漏掉,且常會被錯寫成super.saveMessage(request,messages);
             return mapping.getInputForward();

          步驟2、3、4,與FormBean驗證相同。

          主要區別:ActionBean驗證主要做業務,需要與數據庫交互,比如用戶名是否可用,轉賬余額是否可用,出貨余額等驗證;formBean雖然完成客戶端驗證功能,但需要與服務器交互。注意不同的區分標準。。

           

          本文章大部分內容轉載自 西安云工廠http://www.xaygc.com/struts.html

          posted on 2011-06-03 21:43 aya000 閱讀(251) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 延吉市| 磐石市| 英吉沙县| 白水县| 忻州市| 汽车| 胶州市| 武强县| 建始县| 特克斯县| 海伦市| 会昌县| 周口市| 怀集县| 安平县| 鸡泽县| 廊坊市| 松桃| 达拉特旗| 法库县| 海林市| 金平| 枣强县| 华阴市| 易门县| 永顺县| 中牟县| 斗六市| 礼泉县| 长宁区| 隆化县| 深圳市| 阿荣旗| 治县。| 基隆市| 辽源市| 南澳县| 泽库县| 黔西| 顺平县| 鸡泽县|