`
zc4530
  • 浏览: 84285 次
  • 性别: Icon_minigender_1
  • 来自: 长春
社区版块
存档分类
最新评论

在Struts中如何编写错误处理页面(errorpage.jsp)

阅读更多

在Struts中对于exception出现的情况下,可以使用

代码
  1. <global-exceptions>  
  2.     <exception key="errors.message" type="java.lang.Exception" path="/errorpage.jsp" scope="request"/>  
  3. </global-exceptions>  
<script type="text/javascript">render_code();</script>
来指定错误页面
我希望在页面上显示详细的错误信息,如何编写errorpage.jsp页面呢?
web.xml设定
代码
  1. <!--exception-type设定处理那种异常或子类,location设定捕获到该异常后转向去哪里-->  
  2.     <error-page>  
  3.         <exception-type>org.xiaohanne.simplemvc.exception.ErrorPageException</exception-type>  
  4.         <location>/error/error.jsp</location>  
  5.     </error-page>  
<script type="text/javascript">render_code();</script>
error.jsp 这个页面因为isErrorPage="true"所以能取默认的exception
代码
  1. <%@page contentType = "text/html;charset=gb2312" %>  
  2. <%@page isErrorPage="true"%>  
  3. <html>  
  4. <head>  
  5. <title>错误处理</title>  
  6. <meta http-equiv="Expires" content="">  
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  
  8. <link rel="stylesheet" href="css.css">  
  9. <body color=#FF0000>  
  10. <br><br><br>  
  11. <table width="400" bgcolor=#EEEEFF align=center border="0" cellspacing="0" cellpadding="0">  
  12.   <tr class=th height=22><td colspan=10>&错误提示</td></tr>  
  13.   <tr bgcolor=#CCCCEE height=1><td colspan=10></td></tr>  
  14.   <tr>  
  15.     <td width=30 rowspan="4">&</td>  
  16.     <td>  
  17.          <font color=#0000FF><font color="#0000FF">错误信息为:<%=exception.getMessage()%>  
  18.        <%--根据配置文件或url参数决定是否显示--%>          
  19.         <%String sIsDebug=request.getParameter("debug");   
  20.         if(sIsDebug==null||sIsDebug.equals("")) sIsDebug="false";  
  21.         if(org.xiaohanne.simplemvc.config.Configurator.isDebug()||sIsDebug.equals("true")){%>  
  22.           <hr>   
  23.           错误详细信息(供管理人员调试):<%=exception.toString()%>   
  24.           <hr>   
  25.           错误堆栈(供管理人员调试):<br><%=((org.xiaohanne.simplemvc.exception.ErrorPageException)exception).toTrace()%>          
  26.         <%}%>  
  27.         </font></font>    
  28.     </td>  
  29.     <td height="69">&</td>  
  30.   </tr>  
  31.   <tr height=30 valign=center>   
  32.     <td valign="top" align="right"><a href='javascript:history.go(-1);'>返回上页</a></td>  
  33.   </tr>  
  34.   <tr bgcolor=#CCCCEE height=1><td colspan=10></td></tr>      
  35. </table>  
  36. </body>  
  37. </html> 

补上ErrorPageException.toTrace()的定义

代码
  1. package org.xiaohanne.simplemvc.exception;  
  2.   
  3. /**所有要转到error页处理的Exception的超类,需在web.xml里设定为被捕捉 
  4.  * @author Xiaohanne 
  5.  */  
  6.   
  7. public abstract class ErrorPageException extends ServletException {  
  8.   protected String _errorStr=super.toString();  
  9.     
  10.   public String toTrace(){  
  11.     String s="\n"+this.getClass().toString();  
  12.     StackTraceElement[] trace = this.getStackTrace();  
  13.     for (int i=0; i < trace.length; i++)  
  14.     s+="\n " + trace[i];     
  15.     return s;  
  16.   }  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics