转载/Struts2拦截器实现异常处理机制
2016-04-21 07:55
501 查看
创建ErrorInterceptor.java
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class ErrorInterceptor implements Interceptor {
private static final long serialVersionUID = 1L;
public void init() {
}
public String intercept(ActionInvocation actioninvocation){
// Action的返回值
String result = null;
try {
// 运行被拦截的Action,期间如果发生异常会被catch住
result = actioninvocation.invoke();
return result;
} catch (Exception e) {
//处理异常
String errorMsg = "出现错误信息,请查看日志!";
//通过instanceof判断到底是什么异常类型
if (e instanceof RuntimeException) {
//未知的运行时异常
RuntimeException runtimeException = (RuntimeException) e;
errorMsg = runtimeException.getMessage().trim();
}
//把自定义错误信息
HttpServletRequest request = (HttpServletRequest) actioninvocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
//发送错误消息到页面
request.setAttribute("errorMsg", errorMsg);
//log4j记录日志
Logger logger = Logger.getLogger(actioninvocation.getAction().getClass());
logger.error(errorMsg, e);
return "errorMsg";
}
}
public void destroy() {
}
}
-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------
导入jstl-1.0.jar和standard-1.0.jar包
创建errorMsg.jsp文件,并添加JSTL标签,JSTL异常参考http://blog.csdn.net/isiah_zhou/article/details/51205363。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error Message</title>
</head>
<body>
<c:if test="${requestScope.errorMsg == null}">
<p>对不起,系统发生了未知的错误,请查看日志</p>
</c:if>
<c:if test="${requestScope.errorMsg ne null}">
<p>${requestScope.errorMsg}</p>
</c:if>
</body>
</html>
-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------
配置web.xml
<interceptors>
<!-- 声明拦截器 -->
<interceptor name="errorInterceptor" class="com.shenzhen.management.util.exception.ErrorInterceptor" />
<!-- 配置拦截器栈 -->
<interceptor-stack name="myErrorInterceptor">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="errorInterceptor" />
</interceptor-stack>
</interceptors>
<!-- 覆盖底层的拦截器栈 对包中的所有action都有效 -->
<default-interceptor-ref name="myErrorInterceptor"/>
<global-results>
<result name="errorMsg">/WEB-INF/page/errorMsg.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="errorMsg" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------
原文链接:http://bbs.itcast.cn/thread-10364-1-1.html
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class ErrorInterceptor implements Interceptor {
private static final long serialVersionUID = 1L;
public void init() {
}
public String intercept(ActionInvocation actioninvocation){
// Action的返回值
String result = null;
try {
// 运行被拦截的Action,期间如果发生异常会被catch住
result = actioninvocation.invoke();
return result;
} catch (Exception e) {
//处理异常
String errorMsg = "出现错误信息,请查看日志!";
//通过instanceof判断到底是什么异常类型
if (e instanceof RuntimeException) {
//未知的运行时异常
RuntimeException runtimeException = (RuntimeException) e;
errorMsg = runtimeException.getMessage().trim();
}
//把自定义错误信息
HttpServletRequest request = (HttpServletRequest) actioninvocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
//发送错误消息到页面
request.setAttribute("errorMsg", errorMsg);
//log4j记录日志
Logger logger = Logger.getLogger(actioninvocation.getAction().getClass());
logger.error(errorMsg, e);
return "errorMsg";
}
}
public void destroy() {
}
}
-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------
导入jstl-1.0.jar和standard-1.0.jar包
创建errorMsg.jsp文件,并添加JSTL标签,JSTL异常参考http://blog.csdn.net/isiah_zhou/article/details/51205363。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Error Message</title>
</head>
<body>
<c:if test="${requestScope.errorMsg == null}">
<p>对不起,系统发生了未知的错误,请查看日志</p>
</c:if>
<c:if test="${requestScope.errorMsg ne null}">
<p>${requestScope.errorMsg}</p>
</c:if>
</body>
</html>
-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------
配置web.xml
<interceptors>
<!-- 声明拦截器 -->
<interceptor name="errorInterceptor" class="com.shenzhen.management.util.exception.ErrorInterceptor" />
<!-- 配置拦截器栈 -->
<interceptor-stack name="myErrorInterceptor">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="errorInterceptor" />
</interceptor-stack>
</interceptors>
<!-- 覆盖底层的拦截器栈 对包中的所有action都有效 -->
<default-interceptor-ref name="myErrorInterceptor"/>
<global-results>
<result name="errorMsg">/WEB-INF/page/errorMsg.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="errorMsg" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------
原文链接:http://bbs.itcast.cn/thread-10364-1-1.html
相关文章推荐
- 如何优雅地处理前端异常?
- C#异常处理详解
- 轻松学习C#的异常处理
- PHP异常处理Exception类
- JS异常处理的一个想法(sofish)
- PHP 的异常处理、错误的抛出及回调函数等面向对象的错误处理方法
- PHP如何抛出异常处理错误
- PHP中的错误处理、异常处理机制分析
- js中的异常处理try...catch使用介绍
- php5编程中的异常处理详细方法介绍
- php异常处理使用示例
- Asp.net Mvc 身份验证、异常处理、权限验证(拦截器)实现代码
- javascript 异常处理使用总结
- java多线程中的异常处理机制简析
- 深入理解Java编程中异常处理的优劣
- 分享一个php 的异常处理程序
- 简单了解Java编程中对异常处理的运用
- 深入剖析Java中的各种异常处理方式
- JS中的异常处理方法分享
- 一些.NET对多线程异常处理技巧分享