神秘的 J2ee 殿堂

          ·古之學者必有師·做學者亦要做師者·FIGHTING·

          Struts中的error handling,html:messages 與html:errors的顯示錯誤消息的區別

          在struts中,對Form和Action等錯誤信息在頁面中的顯示非常方便,通過<html:messages/>與<html:errors/>
          標簽都能完成。而他們在語法上的區別也很小,舉例說明:<html:messages/>

                  ActionMessages message = new ActionMessages();
                  
          //從Request范圍獲得出錯頁面的資源文件屬性
                  MessageResources messageResources = getResources(request);
                  
          //判斷是否出錯,出錯就添加到message對象中,其中add的第一個屬性用于標識不同的錯誤信息,第二個屬性是得到錯誤提        示字符串,第一個屬性與顯示頁面的Property對應,如果設為一樣就把錯一起顯示出來
                  if(bookEditForm.getAuthor().equals(""))
                      message.add(
          "author"new ActionMessage(
                      "
          error.field", messageResources.getMessage("label.author", request)));
                  
          if(bookEditForm.getTitle().equals(""))
                      message.add(
          "author2"new ActionMessage(
                      "
          error.field", messageResources.getMessage("label.title", request)));
                  
          //判斷是否有錯誤,并跳轉        
                  if(!message.isEmpty()){
                  //在Request范圍保存錯誤消息
                      saveMessages(request, message);
                      
          return mapping.findForward("showEdit");
                  }
          頁面顯示:
          其中id屬性可以隨便取,但bean:wirte的name要與id一樣,顯示全部錯誤信息就不要Property屬性,顯示特定的出錯信息就要指定property屬性
                      <html:messages id="haha" property="author" message="true"> 
                          
          <font style="font-weight:bold; color=#FF0000">
                          <
          bean:write name="haha" />
                         
          </font><br>
                      
          </html:messages>


          <html:errors/>
                  ActionErrors actionErrors = new ActionErrors();
                  MessageResources messageResources 
          = getResources(request);
                  
                  
          if(bookEditForm.getAuthor().equals(""))
                      actionErrors.add(
          "author"new ActionError(
                     
          "error.field", messageResources.getMessage("label.author", request)));
                  
          if(bookEditForm.getTitle().equals(""))
                      actionErrors.add(
          "author2"new ActionError(
                     
          "error.field", messageResources.getMessage("label.title", request)));

                  
          if(!actionErrors.isEmpty()){
                      saveErrors(request, actionErrors);
                      
          return mapping.findForward("showEdit");
                  }

          頁面顯示:
          <html:errors property="author"/>
          //或者
          <html:errors />

          最后:html:messages是1.1以后出現的,也是推薦使用的;

          posted on 2007-09-13 14:48 月芽兒 閱讀(709) 評論(0)  編輯  收藏 所屬分類: J2EE學習心得

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 慈利县| 宜章县| 屏东市| 鹿泉市| 卢氏县| 龙井市| 邳州市| 那曲县| 江孜县| 长顺县| 石嘴山市| 廉江市| 衡东县| 麻江县| 新邵县| 盐池县| 综艺| 鄂托克前旗| 裕民县| 离岛区| 绥江县| 水城县| 吉首市| 绥棱县| 宜城市| 肃宁县| 盐亭县| 赤峰市| 仲巴县| 临湘市| 买车| 信宜市| 西吉县| 将乐县| 萨嘎县| 崇信县| 吴江市| 监利县| 麻江县| 五家渠市| 永昌县|