在Struts中对于exception出现的情况下,可以使用
代码
- <global-exceptions>
- <exception key="errors.message" type="java.lang.Exception" path="/errorpage.jsp" scope="request"/>
- </global-exceptions>
来指定错误页面
我希望在页面上显示 详细的错误信息,如何编写errorpage.jsp页面呢?
web.xml设定
代码
- <!--exception-type设定处理那种异常或子类,location设定捕获到该异常后转向去哪里-->
- <error-page>
- <exception-type>org.xiaohanne.simplemvc.exception.ErrorPageException</exception-type>
- <location>/error/error.jsp</location>
- </error-page>
error.jsp 这个页面因为isErrorPage="true"所以能取默认的exception
代码
- <%@page contentType = "text/html;charset=gb2312" %>
- <%@page isErrorPage="true"%>
- <html>
- <head>
- <title>错误处理</title>
- <meta http-equiv="Expires" content="">
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <link rel="stylesheet" href="css.css">
- <body color=#FF0000>
- <br><br><br>
- <table width="400" bgcolor=#EEEEFF align=center border="0" cellspacing="0" cellpadding="0">
- <tr class=th height=22><td colspan=10>&错误提示</td></tr>
- <tr bgcolor=#CCCCEE height=1><td colspan=10></td></tr>
- <tr>
- <td width=30 rowspan="4">&</td>
- <td>
- <font color=#0000FF><font color="#0000FF">错误信息为:<%=exception.getMessage()%>
- <%--根据配置文件或url参数决定是否显示--%>
- <%String sIsDebug=request.getParameter("debug");
- if(sIsDebug==null||sIsDebug.equals("")) sIsDebug="false";
- if(org.xiaohanne.simplemvc.config.Configurator.isDebug()||sIsDebug.equals("true")){%>
- <hr>
- 错误详细信息(供管理人员调试):<%=exception.toString()%>
- <hr>
- 错误堆栈(供管理人员调试):<br><%=((org.xiaohanne.simplemvc.exception.ErrorPageException)exception).toTrace()%>
- <%}%>
- </font></font>
- </td>
- <td height="69">&</td>
- </tr>
- <tr height=30 valign=center>
- <td valign="top" align="right"><a href='javascript:history.go(-1);'>返回上页</a></td>
- </tr>
- <tr bgcolor=#CCCCEE height=1><td colspan=10></td></tr>
- </table>
- </body>
- </html>
补上ErrorPageException.toTrace()的定义
代码
- package org.xiaohanne.simplemvc.exception;
- /**所有要转到error页处理的Exception的超类,需在web.xml里设定为被捕捉
- * @author Xiaohanne
- */
- public abstract class ErrorPageException extends ServletException {
- protected String _errorStr=super.toString();
- public String toTrace(){
- String s="\n"+this.getClass().toString();
- StackTraceElement[] trace = this.getStackTrace();
- for (int i=0; i < trace.length; i++)
- s+="\n " + trace[i];
- return s;
- }
- }