<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-8  評論-7  文章-0  trackbacks-0

    盡管Struts框架提供了有效的異常處理機(jī)制,但不能保證處理所有的錯(cuò)誤,這時(shí)Struts框架會把錯(cuò)誤拋給Web容器,在默認(rèn)情況下Web容器會向用戶瀏覽器直接返回原始信息。如果想避免直接讓用戶看到這些原始信息,可以在web.xml中配置<error-page>元素,以下代碼演示了如何避免用戶看到HTTP 404、HTTP 500錯(cuò)誤和Exception異常。

    web.xml:
      <error-page>
        <error-code>404</error-code>
        <location>/exception/error404.jsp</location>
      </error-page>
      <error-page>
        <error-code>500</error-code>
        <location>/exception/error500.jsp</location>
      </error-page>
      <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/exception/default.jsp</location>
      </error-page>
    當(dāng)WEB容器捕獲到exception-type或error-code指定的錯(cuò)誤時(shí)將跳到由location指定的頁面。

    ? 問題:當(dāng)form bean 為動態(tài)bean時(shí),在action中無法對form bean數(shù)據(jù)進(jìn)行驗(yàn)證,因?yàn)閒ormbean沒有具體實(shí)現(xiàn)類。action中無法引用
    ? ActionError/ActionErrors/ActionMessage/ActionMessages:

    有時(shí)候你需要向用戶提供相關(guān)處理信息,包括表單驗(yàn)證時(shí)發(fā)現(xiàn)錯(cuò)誤等。
    1. 相關(guān)類介紹:
    ActionMessage:用于保存一個(gè)與資源束對應(yīng)的提示信息。主要構(gòu)造函數(shù)如:
    ActionMessage(String message);
    ActionMessage(String message,paramater)。

    ActionMessages:用于保存多個(gè)ActionMessage。并在html:errors 和html:messages中起作用。
    主要構(gòu)造函數(shù):
    ActionMessages().
    主要方法是add(String property,ActionMessage message)
    ActionMessages有一個(gè)HashMap類型messages保存多個(gè)ActionMessage對象,每個(gè)ActionMessage對象都有唯一的一個(gè)property標(biāo)識。這個(gè)property可以是自定義的任意字符串,也可以由org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE指定
    html:messages/html:errors使用property屬性訪問某個(gè)資源

    ActionErrors:用于保存一個(gè)與資源束對應(yīng)的錯(cuò)誤信息。用法跟ActionMessages差不多。
    ActionError不贊成使用。


    2. 版本:
    struts1.1中用ActionErrors報(bào)告錯(cuò)誤,用ActionMessages提供信息。
    在struts1.2中使用ActionMessages提供信息和錯(cuò)誤,不贊成使用ActionError
    struts1.3中已經(jīng)沒有ActionError類了。

    3. AtionErrors和ActionMessages的區(qū)別

    1. ActionErrors是ActionMessages的一個(gè)子類,功能幾乎相同,不同點(diǎn)在于標(biāo)簽<html:errors/>和<html:messages>的使用上的區(qū)別。
    html:errors指定了footer和header屬性。默認(rèn)值為errors.header和errors.footer,需要時(shí)可以自己指定。如果資源屬性文件配置了 errors.header和errors.footer,則任何時(shí)候使用html:errors時(shí)開頭和結(jié)尾都是這兩個(gè)屬性對應(yīng)的資源信息。
    而html:message默認(rèn)情況下沒有errors.header和errors.footer值,當(dāng)然可以自己指定。

    2. html:errors可以根據(jù)property屬性指定顯示一個(gè)錯(cuò)誤信息。html:messages有一個(gè)必添項(xiàng)id。html:messages不能直接顯示信息,它將選出的信息放入一個(gè)用id標(biāo)識的Iterator對象里,然后在用ben:write或JSTL c:out標(biāo)簽顯示每個(gè)信息.例如:
    <html:messages message="true" id="msg">
        <c:out value="${msg}"/><br />
    </html:messages>

    3. 具體的一個(gè)例子:
    接受輸入頁面input.jsp:

      <html:form action="/errormessage/input">
        phoneNumber : <html:text property="phoneNumber"/> <html:errors     property="<%=org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE %>"/><br/>
      <html:submit/><html:cancel/>
      </html:form>

    struts-config.xml:
      <form-beans >
        <form-bean name="inputForm" type="cn.rolia.struts.form.errorexception.InputForm" />
      </form-beans>
      <action-mappings >
        <action
          attribute="inputForm"
          input="/errormessage/input.jsp"
          name="inputForm"
          path="/errormessage/input"
          scope="request"
          type="com.yourcompany.struts.action.errormessage.InputAction"
          validate="false">
          <forward name="success" path="/errormessage/success.jsp" />
        </action>
      </action-mappings>

    InputAction.java:

    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
      cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub
      String phoneNumber = inputForm.getPhoneNumber();
      if(phoneNumber.length()<4){
      ActionErrors messages = new ActionErrors();
        messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input"));
        this.saveErrors(request, messages);
        return mapping.getInputForward();
      }

      return mapping.findForward("success");
    }
    解說:用戶輸入手機(jī)號碼,頁面跳轉(zhuǎn)到InputAction控制層進(jìn)行處理,若輸入數(shù)據(jù)小于4,則創(chuàng)建一個(gè)ActionMessage類存儲相關(guān)錯(cuò)誤信息。然后再創(chuàng)建ActionErrors類將此ActionMessage放入ActionErrors。再調(diào)用Action的saveErrors方法將此ActionErrors保存的request范圍里,然后返回input.jsp頁面要求重新輸入并用html:errors提示錯(cuò)誤信息。

    4. Action包含saveErrors()方法和saveMessages()方法。如果創(chuàng)建的ActionErrors則應(yīng)該調(diào)用saveErrors(),若創(chuàng)建的是ActionMessages則應(yīng)該調(diào)用saveMessages()方法。
    saveErrors()接收ActionMessages而不是ActionErrors;同時(shí)將其保存在request中并用一個(gè)由org.apache.struts.Globals.ERROR_KEY指定的常量” org.apache.struts.Globals.ERROR_KEY”標(biāo)識這個(gè)ActionMessages,便于html:errors查找。saveMessages()方法接收ActionMessages同時(shí)將其保存在request中并用一個(gè)由org.apache.struts.Globals.MESSAGE_KEY指定的常量” org.apache.struts.Globals.MESSAGE_KEY”標(biāo)識這個(gè)ActionMessages,進(jìn)而讓html:messages從常量Globals.ERROR_KEY中遍歷獲取信息。可以將其屬性message設(shè)置為true,那么它將從常量Globals.MESSAGE_KEY中遍歷獲取信息。

    5. 默認(rèn)情況下html:messages從
    如果你想將信息保存在session里而不是request,struts1.2提供了
    struts1.1沒有的saveMessages(HttpSession session, ActionMessages messages)方法和saveErrors(javax.servlet.http.HttpSession session, ActionMessages errors)方法。
    InputAction.java:

    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
    cn.rolia.struts.form.errorexception.InputForm inputForm = (cn.rolia.struts.form.errorexception.InputForm) form;// TODO Auto-generated method stub
      String phoneNumber = inputForm.getPhoneNumber();
      if(phoneNumber.length()<4){
        ActionErrors messages = new ActionErrors();
        messages.add(org.apache.struts.action.ActionMessages.GLOBAL_MESSAGE,new ActionMessage("error.errormessage.input"));
        this.saveErrors(request.getSession(true), messages);
        return mapping.getInputForward();
      }

      return mapping.findForward("success");
    }

     

    posted on 2008-05-04 15:00 神未必這么寫 閱讀(2491) 評論(2)  編輯  收藏 所屬分類: think in struts

    評論:
    # re: actionMessages(),actionError()介紹 2008-05-04 15:23 | 天堂一定是圖書館的樣子
    logic:messagesPresent標(biāo)簽用于以下情況:

    在request作用域中存在一個(gè)ActionMessages對象,標(biāo)簽的property屬性和ActionMessages中的property對應(yīng)。
    在request作用域中存在一個(gè)ActionErrors對象,標(biāo)簽的property屬性和ActionErrors中的property對應(yīng)。
    存在一個(gè)String對象,將其轉(zhuǎn)換(構(gòu)造)成ActionMessage然后在添加到ActionMessages中。
    存在一個(gè)String Array對象,將數(shù)組中的每一個(gè)String轉(zhuǎn)換成一個(gè)ActionMessage,在將其添加到ActionMessages中。
    標(biāo)簽的message屬性值為true時(shí)將以Globals.MESSAGE_KEY為key在request作用域中查找Message,其它情況下,將name的值作為key查找,如果name 沒有出現(xiàn),默認(rèn)值為Globals.ERROR_KEY。

    下面的代碼示例了logic:messagesPresent標(biāo)簽的典型用法:

    <logic:messagesPresent>
    <ul>
    <html:messages id="message">
    <li><bean:write name="message"/></li>
    </html:messages>
    </ul>
    </logic:messagesPresent>

    標(biāo)簽logic:messagesNotPresent的應(yīng)用正好和logic:messagesPresent的應(yīng)用相反。

    回頁首
      回復(fù)  更多評論
      
    # re: actionMessages(),actionError()介紹 2008-05-04 15:26 | 天堂一定是圖書館的樣子
    Struts框架中,網(wǎng)頁的錯(cuò)誤標(biāo)簽都是由<html:errors/>標(biāo)簽實(shí)現(xiàn)和負(fù)責(zé)的。
    <html:errors/>標(biāo)簽在request和session的范圍內(nèi)尋找ActionMessages(或者其子類ActionErrors)集合對象,再從ActionMessages集合對象中讀取ActionMessage對象,把ActionMessage對象包含的消息文本顯示到網(wǎng)頁上。

    <html:errors/>標(biāo)簽還有很多屬性,諸如name,propertity,bundle等;
    在向ActionMessages 或者ActionErrors對象中添加子對象(ActionMessage,ActionError)使用的方法是:
    ActionMessages.add(java.lang.String property,ActionMessage message);
    而<html:errors/>標(biāo)簽在讀ActionMessages或者ActionErrors對象時(shí),使用name屬性幫助用戶顯示和String property 相匹配的信息。
    如果<html:errors/>標(biāo)簽沒有規(guī)定name屬性,那么顯示的將是默認(rèn)的 Globals.ERROR_KEY對應(yīng)的消息。


    這個(gè)是我學(xué)習(xí)的一點(diǎn)心得,樓主見笑。
    o saveErrors(request,errors)的意思呢,是將你的errors集合對象存放到request的范圍內(nèi),以便你在jsp頁面中的<html:errors/>讀取和顯示。  回復(fù)  更多評論
      

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: caoporn成人免费公开| 亚洲午夜无码久久久久小说| 亚洲中文字幕无码不卡电影 | 一级毛片a免费播放王色电影| 亚洲乱亚洲乱妇24p| 亚洲色偷偷色噜噜狠狠99网| 亚洲免费福利在线视频| 亚洲人片在线观看天堂无码| 亚洲色最新高清av网站| 亚洲av无码专区青青草原| mm1313亚洲国产精品无码试看| 亚洲av无码一区二区三区人妖| 国产成人+综合亚洲+天堂| 一级毛片一级毛片免费毛片| 久久毛片免费看一区二区三区| 最近免费中文字幕中文高清| 午夜视频免费在线观看| 91香蕉国产线观看免费全集| 中文字幕无码播放免费| 青青草国产免费久久久下载| 免费人成网站7777视频| 久久伊人亚洲AV无码网站| 国产成A人亚洲精V品无码性色| 亚洲精品韩国美女在线| 亚洲一卡2卡3卡4卡乱码 在线| 亚洲av无码一区二区三区在线播放 | 四虎www免费人成| 五月婷婷亚洲综合| 亚洲精品少妇30p| 亚洲另类春色校园小说| 亚洲av无码专区在线观看下载| 一级毛片免费全部播放| 国产午夜精品久久久久免费视 | 国产成人午夜精品免费视频| 真实乱视频国产免费观看 | 一二三四视频在线观看中文版免费 | 67pao强力打造国产免费| 成人爱做日本视频免费| 亚洲日韩中文无码久久| 亚洲国产理论片在线播放| 日本特黄特色AAA大片免费|