各位大侠好:
本人用的是struts2拦截器的声明式异常处理,定义如下:
struts.xml
配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<package name="struts-base" abstract="true" extends="struts-default">
<interceptors>
<interceptor name="exceptionInterceptor" class="exceptionInterceptor"/>
<global-results>
<result name="error" type="redirect">/common/myerror.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="com.hw.exception.SystemException" result="error"/>
</global-exception-mappings>
</package>
</struts>自定义异常
SystemException:
public class SystemException extends RuntimeException {
private static final long serialVersionUID = 1L;
public SystemException(String frdMessage) {
super(createFriendlyErrMsg(frdMessage));
}
public SystemException(Throwable throwable){
super(throwable);
}
public SystemException(Throwable throwable, String frdMessage){
super(throwable);
}
private static String createFriendlyErrMsg(String msgBody) {
String prefixStr = "抱歉。";
String suffixStr = "请稍后再试或与管理员联系!";
StringBuffer friendlyErrMsg = new StringBuffer();
friendlyErrMsg.append(prefixStr);
friendlyErrMsg.append(msgBody);
friendlyErrMsg.append(suffixStr);
return friendlyErrMsg.toString();
}
}错误页面:
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);
response.setHeader("Pragma","no-cache");
%>
<html>
<head>
<script language="javascript">
function showContent(){
if(document.getElementById("errorMessage").style.display == 'block'){
document.getElementById("errorMessage").style.display = 'none';
}else{
document.getElementById("errorMessage").style.display = 'block';
}
}
</script>
</head>
<body scroll="auto">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="bg" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="table-layout:fixed;word-break:break-all;">
<tr>
<td align="center" width="100%" height="80">
</td>
</tr>
<tr>
<td height="30" align="center">
<a href="#" onclick="javascript:history.go(-1);"><s:text name="global.return"/></a>
<a href="#" onclick="javascript:showContent();">查看详细信息</a>
</td>
</tr>
<tr>
<td align="left" valign="top">
<!-- 异常堆栈信息(开发人员用) -->
<div style="display:none;" id="errorMessage">
<pre> <s:property value="exceptionStack" /></pre>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>拦截器定义:
public class ExceptionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -2871235590759171276L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String result = "";
try {
result = invocation.invoke();
} catch (DataAccessException ex) {
throw new SystemException(ex,"数据库操作失败!");
} catch (NullPointerException ex) {
throw new SystemException(ex,"空指针,调用了未经初始化或者是不存在的对象!");
} catch (IOException ex) {
throw new SystemException(ex,"IO读写异常!");
} catch (ClassNotFoundException ex) {
throw new SystemException(ex,"指定的类不存在!");
} catch (ArithmeticException ex) {
throw new SystemException(ex,"数学运算异常!");
} catch (ArrayIndexOutOfBoundsException ex) {
throw new SystemException(ex,"数组下标越界!");
} catch (IllegalArgumentException ex) {
throw new SystemException(ex,"调用方法的参数错误!");
} catch (ClassCastException ex) {
throw new SystemException(ex,"类型强制转换错误!");
} catch (SecurityException ex) {
throw new SystemException(ex,"违背安全原则异常!");
} catch (SQLException ex) {
throw new SystemException(ex,"操作数据库异常!");
} catch (NoSuchMethodError ex) {
throw new SystemException(ex,"调用了未定义的方法!");
} catch (InternalError ex) {
throw new SystemException(ex,"Java虚拟机发生了内部错误!");
} catch (Exception ex) {
throw new SystemException(ex,"程序内部错误,操作失败!");
}
System.out.print("===测试数据=====");
if(StringHelper.isEmpty(result)){
result = "error";
}
return result;
}
}
异常抛出后不能显示页码
本人用的是struts2拦截器的声明式异常处理,定义如下:
struts.xml
配置
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"><struts>
<package name="struts-base" abstract="true" extends="struts-default">
<interceptors>
<interceptor name="exceptionInterceptor" class="exceptionInterceptor"/>
<global-results>
<result name="error" type="redirect">/common/myerror.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="com.hw.exception.SystemException" result="error"/>
</global-exception-mappings>
</package>
</struts>自定义异常
SystemException:
public class SystemException extends RuntimeException {
private static final long serialVersionUID = 1L;
public SystemException(String frdMessage) {
super(createFriendlyErrMsg(frdMessage));
}
public SystemException(Throwable throwable){
super(throwable);
}
public SystemException(Throwable throwable, String frdMessage){
super(throwable);
}
private static String createFriendlyErrMsg(String msgBody) {
String prefixStr = "抱歉。";
String suffixStr = "请稍后再试或与管理员联系!";
StringBuffer friendlyErrMsg = new StringBuffer();
friendlyErrMsg.append(prefixStr);
friendlyErrMsg.append(msgBody);
friendlyErrMsg.append(suffixStr);
return friendlyErrMsg.toString();
}
}错误页面:
<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page isErrorPage="true"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
response.setHeader("Cache-Control","no-cache");
response.setHeader("Cache-Control","no-store");
response.setDateHeader("Expires",0);
response.setHeader("Pragma","no-cache");
%>
<html>
<head>
<script language="javascript">
function showContent(){
if(document.getElementById("errorMessage").style.display == 'block'){
document.getElementById("errorMessage").style.display = 'none';
}else{
document.getElementById("errorMessage").style.display = 'block';
}
}
</script>
</head>
<body scroll="auto">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" class="bg" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="table-layout:fixed;word-break:break-all;">
<tr>
<td align="center" width="100%" height="80">
</td>
</tr>
<tr>
<td height="30" align="center">
<a href="#" onclick="javascript:history.go(-1);"><s:text name="global.return"/></a>
<a href="#" onclick="javascript:showContent();">查看详细信息</a>
</td>
</tr>
<tr>
<td align="left" valign="top">
<!-- 异常堆栈信息(开发人员用) -->
<div style="display:none;" id="errorMessage">
<pre> <s:property value="exceptionStack" /></pre>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>拦截器定义:
public class ExceptionInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = -2871235590759171276L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
String result = "";
try {
result = invocation.invoke();
} catch (DataAccessException ex) {
throw new SystemException(ex,"数据库操作失败!");
} catch (NullPointerException ex) {
throw new SystemException(ex,"空指针,调用了未经初始化或者是不存在的对象!");
} catch (IOException ex) {
throw new SystemException(ex,"IO读写异常!");
} catch (ClassNotFoundException ex) {
throw new SystemException(ex,"指定的类不存在!");
} catch (ArithmeticException ex) {
throw new SystemException(ex,"数学运算异常!");
} catch (ArrayIndexOutOfBoundsException ex) {
throw new SystemException(ex,"数组下标越界!");
} catch (IllegalArgumentException ex) {
throw new SystemException(ex,"调用方法的参数错误!");
} catch (ClassCastException ex) {
throw new SystemException(ex,"类型强制转换错误!");
} catch (SecurityException ex) {
throw new SystemException(ex,"违背安全原则异常!");
} catch (SQLException ex) {
throw new SystemException(ex,"操作数据库异常!");
} catch (NoSuchMethodError ex) {
throw new SystemException(ex,"调用了未定义的方法!");
} catch (InternalError ex) {
throw new SystemException(ex,"Java虚拟机发生了内部错误!");
} catch (Exception ex) {
throw new SystemException(ex,"程序内部错误,操作失败!");
}
System.out.print("===测试数据=====");
if(StringHelper.isEmpty(result)){
result = "error";
}
return result;
}
}
异常抛出后不能显示页码
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货