Struts2備忘錄【顯示web異常】

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

          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已經(jīng)是自定義的GUIException(在struts.xml中配置過(guò)了),所以直接從里面取信息(args,errCode...)

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

          總結(jié):
            還是利用了struts2的interceptor機(jī)制,方便的攔截了異常.

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

          評(píng)論

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

          好  回復(fù)  更多評(píng)論   

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


            回復(fù)  更多評(píng)論   

          公告


          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(13)

          我參與的團(tuán)隊(duì)

          隨筆分類(69)

          隨筆檔案(68)

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 南召县| 繁昌县| 体育| 赤壁市| 莆田市| 桃源县| 都匀市| 华宁县| 湄潭县| 凭祥市| 南乐县| 锡林郭勒盟| 如东县| 靖州| 吉首市| 新密市| 涞源县| 资中县| 林西县| 晋宁县| 扎鲁特旗| 江城| 来安县| 和田县| 司法| 常州市| 梁山县| 昂仁县| 南郑县| 芮城县| 林甸县| 钟祥市| 吉首市| 蕉岭县| 云南省| 信宜市| 林甸县| 潞城市| 民勤县| 雅安市| 象山县|