Struts2備忘錄【顯示web異常】
在項目中采用了struts2的<global-results>&<global-exception-mappings>處理異常,并在web頁面上顯示異常,在這里備忘一下
1.在strtus.xml中配置如下
2.完成systemFail.jsp和headerMsg.jsp,headerMsg顯示的是知道的Exception,systemFail.jsp顯示unhandle的Exception
3. 在Action中trhows GuiException,然后strtus2會默認使用exception的interceptor將Action中拋出的GUIException攔截,然后匹配xml文件,這樣就完成了錯誤處理.
總結:
還是利用了struts2的interceptor機制,方便的攔截了異常.
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<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>
2.完成systemFail.jsp和headerMsg.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...)<%@ 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>
3. 在Action中trhows GuiException,然后strtus2會默認使用exception的interceptor將Action中拋出的GUIException攔截,然后匹配xml文件,這樣就完成了錯誤處理.
總結:
還是利用了struts2的interceptor機制,方便的攔截了異常.
posted on 2007-07-18 17:19 想飛就飛 閱讀(4582) 評論(2) 編輯 收藏 所屬分類: J2EE