struts2全域性異常處理
阿新 • • 發佈:2019-01-24
struts.xml配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.ui.theme" value="simple"/> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <package name="default" namespace="/" extends="struts-default"> <!-- 全域性結果檢視 --> <global-results> <result name="error">/WEB-INF/pages/error.jsp</result> </global-results> <!-- struts2異常處理框架 --> <global-exception-mappings> <exception-mapping result="error" exception="cn.itcast.jk.exception.SysException"></exception-mapping> <exception-mapping result="error" exception="java.lang.Exception"></exception-mapping> </global-exception-mappings> <action name="login" method="login" class="loginAction"> <result name="login">/WEB-INF/pages/sysadmin/login/login.jsp</result> <result name="success">/WEB-INF/pages/home/fmain.jsp</result> </action> <action name="logout" method="logout" class="loginAction"> <result name="logout">/WEB-INF/pages/sysadmin/login/logout.jsp</result> </action> <action name="homeAction_*" method="{1}" class="homeAction"> <result name="fmain">/WEB-INF/pages/home/fmain.jsp</result> <result name="title">/WEB-INF/pages/home/title.jsp</result> <result name="toleft">/WEB-INF/pages/${moduleName}/left.jsp</result> <result name="tomain">/WEB-INF/pages/${moduleName}/main.jsp</result> </action> </package> <!-- 分模組開發 --> <include file="struts/struts-sysadmin.xml"></include> <include file="struts/struts-cargo.xml"></include> <include file="struts/struts-stat.xml"></include> </struts>
自定義exception
package cn.itcast.jk.exception;
/**
* 異常處理類
* @author Administrator
*
*/
public class SysException extends Exception {
private String message;
public String getMessage() {
return message;
}
public SysException(String message){
this.message = message;
}
}
error.jsp
<%@ page contentType="text/html;charset=UTF-8" isErrorPage="true" %> <%@taglib prefix="s" uri="/struts-tags"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Error Page</title> <script language="javascript"> function showDetail() { var elm = document.getElementById('detail_system_error_msg'); if(elm.style.display == '') { elm.style.display = 'none'; }else { elm.style.display = ''; } } </script> </head> <body style="font-family:微軟雅黑;"> <div id="content" style="text-align:left;"> <table> <tr> <td><img alt="system internal error" src="${pageContext.request.contextPath }/images/error01.jpg"/></td> <br> <b>錯誤資訊:</b> <div style="color:blue;padding:15px;"> <s:property value="exception.message"/> </div> <button onclick="history.back();">返回</button> <p><a href="#" onclick="showDetail();">點選這裡檢視具體錯誤訊息</a>, <br/> 報告以下錯誤訊息給系統管理員,可以更加快速的解決問題; <br/>聯絡電話:120 </p> </td> </tr> </table> <div id="detail_system_error_msg" style="display:none;text-align:left;padding-bottom:100px;"> <pre><s:property value="exceptionStack"/></pre> </div> </div> </body> </html>