神秘的 J2ee 殿堂

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

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

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

                  ActionMessages message = new ActionMessages();
                  
          //從Request范圍獲得出錯(cuò)頁面的資源文件屬性
                  MessageResources messageResources = getResources(request);
                  
          //判斷是否出錯(cuò),出錯(cuò)就添加到message對(duì)象中,其中add的第一個(gè)屬性用于標(biāo)識(shí)不同的錯(cuò)誤信息,第二個(gè)屬性是得到錯(cuò)誤提        示字符串,第一個(gè)屬性與顯示頁面的Property對(duì)應(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 月芽?jī)?/a> 閱讀(702) 評(píng)論(0)  編輯  收藏 所屬分類: J2EE學(xué)習(xí)心得

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          相冊(cè)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 高唐县| 海口市| 五家渠市| 武强县| 桑植县| 宣武区| 大洼县| 曲周县| 苏尼特右旗| 长治市| 固安县| 庆城县| 报价| 叙永县| 靖西县| 昔阳县| 修文县| 江源县| 绥阳县| 凤城市| 梁平县| 芜湖市| 应用必备| 东安县| 台湾省| 方城县| 天全县| 清苑县| 台安县| 周至县| 陵水| 松阳县| 红安县| 固始县| 修文县| 神农架林区| 福贡县| 化州市| 大理市| 钟祥市| 伊川县|