/xml配置文件/struts2的配置/struts2全局异常处理.txt
=====================================================================
1、baseaction加上注解 @ParentPackage("default")
=====================================================================
2、strutx.xml配置如下:SysException可以不创建,这样错误的信息(jsp页面的exception.message)是英文,可以自定义异常SysException,代码在第4点。
<?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>
<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="error">/WEB-INF/pages/error.jsp</result>
</global-results>
<!-- global-exception-mappings:全局异常处理
java.lang.Exception是最大的异常(Exception) -->
<global-exception-mappings>
<!-- <exception-mapping result="error" exception="cn.me.exception.SysException"></exception-mapping> -->
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
</struts>">
=====================================================================
3、/WEB-INF/pages/error.jsp内容如下:重点是exceptionStack可以获取错误的堆栈信息
<%@ 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 οnclick="history.back();">返回</button>
<p><a href="#" οnclick="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>
=====================================================================
4、cn.me.exception.SysException代码如下:自定义异常要自己抛,代码见5
package cn.me.exception;
public class SysException extends Exception {
public SysException(String message) {
super(message);
}
}
=====================================================================
5、抛自定义的异常:
@Action(value = "roleAction_toview", results = {
@Result(name = "view", location = "/WEB-INF/pages/sysadmin/role/jRoleView.jsp") })
public String view() throws SysException {
String id = d.getId();
Role d1;
try {
d1 = ds.get(id);
} catch (Exception e) {
throw new SysException("请勾选一个");
}
push(d1);
return "view";
}
=====================================================================
1、baseaction加上注解 @ParentPackage("default")
=====================================================================
2、strutx.xml配置如下:SysException可以不创建,这样错误的信息(jsp页面的exception.message)是英文,可以自定义异常SysException,代码在第4点。
<?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>
<package name="default" namespace="/" extends="struts-default">
<global-results>
<result name="error">/WEB-INF/pages/error.jsp</result>
</global-results>
<!-- global-exception-mappings:全局异常处理
java.lang.Exception是最大的异常(Exception) -->
<global-exception-mappings>
<!-- <exception-mapping result="error" exception="cn.me.exception.SysException"></exception-mapping> -->
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
</struts>">
=====================================================================
3、/WEB-INF/pages/error.jsp内容如下:重点是exceptionStack可以获取错误的堆栈信息
<%@ 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 οnclick="history.back();">返回</button>
<p><a href="#" οnclick="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>
=====================================================================
4、cn.me.exception.SysException代码如下:自定义异常要自己抛,代码见5
package cn.me.exception;
public class SysException extends Exception {
public SysException(String message) {
super(message);
}
}
=====================================================================
5、抛自定义的异常:
@Action(value = "roleAction_toview", results = {
@Result(name = "view", location = "/WEB-INF/pages/sysadmin/role/jRoleView.jsp") })
public String view() throws SysException {
String id = d.getId();
Role d1;
try {
d1 = ds.get(id);
} catch (Exception e) {
throw new SysException("请勾选一个");
}
push(d1);
return "view";
}