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

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

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

    Java綠地(~ming~)

    Java 草地

    常用鏈接

    統計

    最新評論

    struts中AtionErrors和ActionMessages的區別

    盡管Struts框架提供了有效的異常處理機制,但不能保證處理所有的錯誤,這時Struts框架會把錯誤拋給Web容器,在默認情況下Web容器會向用戶瀏覽器直接返回原始信息。如果想避免直接讓用戶看到這些原始信息,可以在web.xml中配置<error-page>元素,以下代碼演示了如何避免用戶看到HTTP 404、HTTP 500錯誤和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>
    當WEB容器捕獲到exception-type或error-code指定的錯誤時將跳到由location指定的頁面。

    ? 問題:當form bean 為動態bean時,在action中無法對form bean數據進行驗證,因為formbean沒有具體實現類。action中無法引用
    ? ActionError/ActionErrors/ActionMessage/ActionMessages:

    有時候你需要向用戶提供相關處理信息,包括表單驗證時發現錯誤等。
    1. 相關類介紹:
    ActionMessage:用于保存一個與資源束對應的提示信息。主要構造函數如:
    ActionMessage(String message);
    ActionMessage(String message,paramater)。

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

    ActionErrors:用于保存一個與資源束對應的錯誤信息。用法跟ActionMessages差不多。
    ActionError不贊成使用。


    2. 版本:
    struts1.1中用ActionErrors報告錯誤,用ActionMessages提供信息。
    在struts1.2中使用ActionMessages提供信息和錯誤,不贊成使用ActionError
    struts1.3中已經沒有ActionError類了。

    3. AtionErrors和ActionMessages的區別

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

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

    3. 具體的一個例子:
    接受輸入頁面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");
    }
    解說:用戶輸入手機號碼,頁面跳轉到InputAction控制層進行處理,若輸入數據小于4,則創建一個ActionMessage類存儲相關錯誤信息。然后再創建ActionErrors類將此ActionMessage放入ActionErrors。再調用Action的saveErrors方法將此ActionErrors保存的request范圍里,然后返回input.jsp頁面要求重新輸入并用html:errors提示錯誤信息。

    4. Action包含saveErrors()方法和saveMessages()方法。

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

    5. 默認情況下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 2007-07-13 19:40 mrklmxy 閱讀(1199) 評論(0)  編輯  收藏


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


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品乱码久久久久蜜桃| 日本特黄特色AAA大片免费| 免费人成在线视频| 边摸边脱吃奶边高潮视频免费| 亚洲人成中文字幕在线观看| 国产高清免费视频| 色吊丝免费观看网站| 91在线精品亚洲一区二区| 日本高清免费不卡在线| 国精产品一区一区三区免费视频| 亚洲va在线va天堂成人| 国产亚洲色婷婷久久99精品91| 成人免费视频网站www| 九九全国免费视频| wwwxxx亚洲| 亚洲国产成人高清在线观看| 精品国产免费一区二区| 99久久人妻精品免费一区| 污污免费在线观看| 亚洲一区二区三区乱码在线欧洲| 亚洲人成人网站色www| 可以免费看的卡一卡二| 午夜在线亚洲男人午在线| 2022年亚洲午夜一区二区福利 | 亚洲国产日韩精品| 亚洲精品无码久久久久sm| 高清国语自产拍免费视频国产 | 四虎在线免费视频| 在线成人精品国产区免费| 色屁屁在线观看视频免费| 亚洲中文字幕人成乱码| 国产V亚洲V天堂A无码| 亚洲国产精品毛片av不卡在线| 99热在线精品免费全部my| 99久久精品国产免费| 久久久久久久久久久免费精品 | aaa毛片视频免费观看| 日韩精品亚洲专区在线影视| 亚洲一区二区三区久久| 亚洲综合精品一二三区在线| 国产亚洲综合成人91精品|