Struts2備忘錄【顯示web異常】

          在項目中采用了struts2的<global-results>&<global-exception-mappings>處理異常,并在web頁面上顯示異常,在這里備忘一下

          1.在strtus.xml中配置如下 
            <global-results>
              
          <result name="unhandledException">/common/systemFail.jsp</result>
              
          <result name="input">/common/headerMsg.jsp</result>
           
          </global-results>

           
          <global-exception-mappings>
              
          <exception-mapping exception="java.lang.Exception" result="unhandledException"/>
              
          <exception-mapping exception="com.xxxx.exception.GuiException" result="input"/>
           
          </global-exception-mappings>
          GuiException是自定義的Exception

          2.完成systemFail.jspheaderMsg.jsp,headerMsg顯示的是知道的Exception,systemFail.jsp顯示unhandle的Exception
          systemFail.jsp
          <%@ include file="../common/header.jsp" %>
            <div style="padding-top:30px">

              
          </div>
              
          <div id="systemFailed" style="text-align:left; padding-left:80px; padding-right:80px;">
                  
          <s:property value="exception"/>
              
          </div>
              
          <br/>
              
          <div style="text-align:left; padding-left:80px; padding-right:80px;">
                  
          <s:property value="exceptionStack"/>
              
          </div>
          <%@ include file="../common/footer.jsp" %>


          headerMsg.jsp
          <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
          <%@ taglib prefix="s" uri="/struts-tags" %>

          <div id="errorMessage" class="errorMessageText">
          <s:actionerror/>
          <s:actionmessage/>

          <s:if test="exception != null">
              <s:if test="exception.errorCode != null">
                  <s:set name="errorCode" value="exception.errorCode"/>
                  <s:set name="args" value="exception.args"/>
                  <s:text name="${errorCode}">
                      <c:forEach var="arg" items="${args}">
                          <s:param>${arg}</s:param>
                      </c:forEach>
                  </s:text>
              </s:if>
              <s:else>
                  <s:property value="exception.message"/>
              </s:else>
          </s:if>
          </div>
          這里的Exception已經是自定義的GUIException(在struts.xml中配置過了),所以直接從里面取信息(args,errCode...)

          3. 在Action中trhows
          GuiException,然后strtus2會默認使用exception的interceptor將Action中拋出的GUIException攔截,然后匹配xml文件,這樣就完成了錯誤處理.

          總結:
            還是利用了struts2的interceptor機制,方便的攔截了異常.

          posted on 2007-07-18 17:19 想飛就飛 閱讀(4582) 評論(2)  編輯  收藏 所屬分類: J2EE

          評論

          # re: Struts2備忘錄【顯示web異常】 2009-05-11 00:31 1171821987

          好  回復  更多評論   

          # re: Struts2備忘錄【顯示web異常】 2009-05-11 00:32 1171821987


            回復  更多評論   

          公告


          導航

          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234

          統計

          常用鏈接

          留言簿(13)

          我參與的團隊

          隨筆分類(69)

          隨筆檔案(68)

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 四子王旗| 监利县| 宁陵县| 浏阳市| 融水| 太原市| 乌拉特前旗| 兰考县| 洪湖市| 宁津县| 壤塘县| 义马市| 徐州市| 彰化市| 哈密市| 洛宁县| 阜城县| 毕节市| 全椒县| 陇西县| 沂南县| 平塘县| 扬州市| 年辖:市辖区| 海口市| 丹凤县| 兴安盟| 沂源县| 十堰市| 灵台县| 甘肃省| 当雄县| 永靖县| 博野县| 安国市| 德惠市| 南投市| 蒙自县| 远安县| 克东县| 乌兰县|