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 想飞就飞 阅读(4561) 评论(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)

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜