神秘的 J2ee 殿堂

          ·古之學(xué)者必有師·做學(xué)者亦要做師者·FIGHTING·

          Struts中的error handling,html:messages 與html:errors的顯示錯(cuò)誤消息的區(qū)別

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

                  ActionMessages message = new ActionMessages();
                  
          //從Request范圍獲得出錯(cuò)頁面的資源文件屬性
                  MessageResources messageResources = getResources(request);
                  
          //判斷是否出錯(cuò),出錯(cuò)就添加到message對象中,其中add的第一個(gè)屬性用于標(biāo)識不同的錯(cuò)誤信息,第二個(gè)屬性是得到錯(cuò)誤提        示字符串,第一個(gè)屬性與顯示頁面的Property對應(yīng),如果設(shè)為一樣就把錯(cuò)一起顯示出來
                  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)));
                  
          //判斷是否有錯(cuò)誤,并跳轉(zhuǎn)        
                  if(!message.isEmpty()){
                  //在Request范圍保存錯(cuò)誤消息
                      saveMessages(request, message);
                      
          return mapping.findForward("showEdit");
                  }
          頁面顯示:
          其中id屬性可以隨便取,但bean:wirte的name要與id一樣,顯示全部錯(cuò)誤信息就不要Property屬性,顯示特定的出錯(cuò)信息就要指定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以后出現(xiàn)的,也是推薦使用的;

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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 丽水市| 西安市| 东港市| 五华县| 许昌市| 金川县| 牙克石市| 建宁县| 闽清县| 三门峡市| 平罗县| 道真| 温泉县| 乌苏市| 尼木县| 秭归县| 土默特左旗| 菏泽市| 宁海县| 金山区| 北碚区| 政和县| 霍州市| 清丰县| 竹北市| 邵阳县| 张北县| 京山县| 潞西市| 尼勒克县| 樟树市| 巴中市| 黄大仙区| 滁州市| 玉林市| 桂林市| 开阳县| 宜章县| 民权县| 绥化市| 海兴县|