转载/Struts2拦截器实现异常处理机制

来源:互联网 发布:博泰与创业软件 编辑:程序博客网 时间:2024/06/06 01:10

创建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

0 0
原创粉丝点击