自由飛翔

          我在仰望,java之上

          統(tǒng)計(jì)

          留言簿(2)

          我關(guān)注的blog

          閱讀排行榜

          評(píng)論排行榜

          ActionMessage與ActionError

          參考文章:http://hi.baidu.com/develop_skill/blog/item/e09b2d8664cddf2dc75cc36b.html

           如果是想獲取error的話,name = org.apache.struts.action.ERROR
           如果是想獲取message的話, name = org.apache.struts.action.ACTION_MESSAGE

          1.src目錄下新建屬性文件HtmlErrors.properties,里面有一些key=walue
          2.
          在struts-config.xml中加入 <message-resources key="HtmlErrors" parameter="HtmlErrors"/>
          3.
          在action類中聲明ActionMessages 屬性,并且在setServlet方法 中初始化之 messages = new ActionMessages();
          在execute中,添加message:
          messages.add("message",new ActionMessage("key")); this.saveErrors(request,messages);//注意此處必須是saveErrors,用saveMessages無效
          4.
          在jsp文件中加入:
          <logic:messagesPresent> <html:errors property="message" bundle="HtmlErrors"> </html:errors> </logic:messagesPresent>

          ActionMessages以一個(gè)HashMap存儲(chǔ)ActionMessage.Map中的key是一個(gè)標(biāo)識(shí),其對(duì)應(yīng)的value是一個(gè)List對(duì)象,所有的ActionMessage存儲(chǔ)在List中.也就是說key標(biāo)識(shí)了一組ActionMessage.

          • ActionMessage 
            • ActionMessage(String key)
              該方法接受一個(gè)字符串,字符串是在資源文件種配置的key值,必須在配置文件中進(jìn)行相關(guān)配置.
            • ActionMessage(String key,Object value)
              ActionMessage(String key,Object value0,Object value1)
              ActionMessage(String key,Object value0,Object value1,Object value2)
              ActionMessage(String key,Object value0,Object value1,Object value2,Object value3)
              上面4個(gè)方法第一個(gè)參數(shù)同樣是資源文件中配置的key值,同樣必須在配置文件中進(jìn)行相關(guān)配置.后面的參數(shù)為資源文件中key所對(duì)應(yīng)的信息中需要的參數(shù)
            • ActionMessage(String key,Object[] values)
              這種方法第一個(gè)參數(shù)同上,第二個(gè)參數(shù)接受一個(gè)Object數(shù)組,其中保存key在資源文件中對(duì)應(yīng)信息需要的參數(shù).
            • ActionMessage(String msg,false 這個(gè)方法顯示自定義消息,即消息輸出內(nèi)容為msg。
          • ActionMessages
            • ActionMessages.add(String property,ActionMessage message)
              第一個(gè)參數(shù)property對(duì)應(yīng)于<html:messages>標(biāo)簽中的property屬性
              , property的值一般采用ActionMessages類中的靜態(tài)常量ActionMessages.GLOBAL_MESSAGE,也可以自己定義一個(gè)key.
            • 該方法執(zhí)行時(shí),先判斷ActionMessages中有沒有該key,如果沒有就新添加對(duì)key-List鍵值對(duì);如果有同樣的key,就先獲取該key對(duì)應(yīng)的Value并轉(zhuǎn)換為List對(duì)象,然后將(ActionMessage)message添加進(jìn)List對(duì)象中.
            • ActionMessages.add(ActionMessages ams)
              該方法將參數(shù)中保存的ActionMessage合并到調(diào)用ActionMessages中.
          • Action及其所有子類
            • addMessages(HttpServletRequest request,ActionMessages messages)
              該方法首先檢查request對(duì)象中是否有ActionMessages對(duì)象,如果有對(duì)象,則將接收的ActionMessages合并到request中,如果沒有,用setAttribute(Globals.MESSAGE_KEY,messages)方法將messages添加進(jìn)去.如果一個(gè)Action中需要顯示多條錯(cuò)誤信息,推薦使用該方法.
              (Globals.MESSAGE_KEY ="org.apache.struts.action.ACTION_MESSAGE")
            • saveMessages(HttpServletRequest request,ActionMessages messages)
              該方法保存messages時(shí),如果request中已經(jīng)有一個(gè)ActionMessages對(duì)象,則用新的覆蓋原有的.不推薦使用該方法,該方法容易發(fā)生丟失信息的情況.
            • addErrors()和saveErrors()
              這兩個(gè)方法與addMessages()和saveMessages()相似,不過在HttpServletRequest.setAttribute()時(shí)的參數(shù)不一樣,這兩個(gè)方法是:setAttribute(Globals.ERROR_KEY,messages)添加的.(Globals.ERROR_KEY = "org.apache.struts.action.ERROR")
                     在request中添加的ActionMessages在頁面顯示時(shí),用<html:messages>標(biāo)簽進(jìn)行顯示, <html:messages>
          id必須指定id屬性.<html:messages>標(biāo)簽作用是遍歷ActionMessages對(duì)象中的所有     ActionMessage.類似<logic:iterator>,每次循環(huán)都定義一個(gè)名稱為id指定的頁面范圍的bean,然后用     <bean:write>標(biāo)簽進(jìn)行輸出.
          bundle綁定一個(gè)MessageResources,不指定的時(shí)候從上下文中配置的資源中檢索.一般不指定bundle屬性.
          locale指定本地化信息,不指定時(shí)和Session的本地化信息相同,一般不用指定.
          name指定ActionMessages對(duì)象在request中的key.不指定時(shí),默認(rèn)用GLOBALS.ERROR_KEY="org.apache.struts.action.ERROR",用Action.addMessages()方法添加的信息將不被顯示,用Action.addErrors()方法添加的信息才會(huì)被顯示.一般不指定.
          property指定哪些ActionMessage將被顯示,與ActionMessages.addMessage(String property,ActionMessage am)中的property參數(shù)相對(duì)應(yīng).不指定的時(shí)候顯示所有信息.
          header指定一個(gè)資源信息key,在顯示ActionMessages前打印到頁面上,可選.
          footer指定一個(gè)資源信息key,在顯示完ActionMessages后打印到頁面上,可選.
          message

          取值為true/false,默認(rèn)為false.為false或不指定時(shí),在request中查找key=Globals.ERROR_KEY的ActionMessages bean.當(dāng)該屬性設(shè)置為true時(shí),在request中查找key=Globals.MESSAGE_KEY的ActionMessages bean,同時(shí)name屬性的值將被忽略. 
          注:true顯示 this.addMessages(request,messages); 里面的消息,
          false顯示 this.addErrors(request, messages)的消息。

           <html:messages>的message屬性如果設(shè)定為true,會(huì)輸出ActionMessages中所儲(chǔ)存的訊息,Message表示一個(gè)提示訊息,也許使用者輸入了不正確的資訊,例如在輸入名稱與密碼時(shí)打錯(cuò)了字,程式要提示使用者他們輸入了不正確的訊息。 
              <html:messages>的message屬性如果不設(shè)定為true,會(huì)輸出ActionErrors中所儲(chǔ)存的訊息,Error代表的是一個(gè)操作方面的錯(cuò)誤,例如錯(cuò)誤操作導(dǎo)致使用者名稱或密碼為空(當(dāng)然也許也是故意的)。

          示例:(struts 1.3)

          ActionForm中:

          ActionMessages messages = new ActionMessages();
              messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("messages.username.required"));
              addMessages(request,messages);
             
              ActionErrors errors = new ActionErrors();
              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("errors.username.required"));
              addErrors(request,errors);
             
              return mapping.getInputForward();

          ===========================================================================

          JSP頁面顯示:

          <html:messages id="outMessage" message="true" header="messages.header" footer="messages.footer">
              <bean:write name="outMessage"/>
             </html:messages>
             <html:messages id="outError" message="false" header="errors.header" footer="errors.footer">
              <bean:write name="outError"/>
             </html:messages>

          ============================================================================

          ApplicationResources.properties配置文件:

          messages.header=<h2><font color="red">
          messages.footer=</font></h2>
          messages.username.required=ActionMessage:the name is null

          errors.header=<h2><font color="blue">
          errors.footer=</font></h2>
          errors.username.required=ActionError:the name is null




          Gavin

          posted on 2011-09-29 11:59 GavinMiao 閱讀(1241) 評(píng)論(0)  編輯  收藏 所屬分類: struts1


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 万源市| 阿拉尔市| 湘乡市| 岳西县| 交城县| 明光市| 全州县| 赫章县| 锡林浩特市| 东辽县| 嘉祥县| 昌平区| 安溪县| 定陶县| 莱芜市| 临沭县| 伊春市| 河曲县| 论坛| 彰武县| 深泽县| 德格县| 冷水江市| 烟台市| 磴口县| 大渡口区| 台南县| 宕昌县| 高密市| 葵青区| 崇仁县| 武功县| 二连浩特市| 马公市| 库伦旗| 张家港市| 平原县| 琼结县| 新绛县| 五寨县| 双城市|