您的位置:首页 > 编程语言 > Java开发

Struts2自定义异常拦截器

2012-01-17 15:19 405 查看
首先自定一个ExceptionManager类,该类继承自RuntimeException,属于Unchecked类型异常。

/*
* file name:ExceptionManager.java
* package:sigma.common.exception
* class name:ExceptionManager
* create time:2012-01-17
* Copyright: Copyright (c) 2012
*/

package sigma.common.exception;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
* 自定义异常类<br>
* 这个<code>ExceptionManager</code>类继承自<code>RuntimeException</code>,属于unchecked类型异常。<br>
* 此类用于统一打包所有的<code>Throwable</code>实例和对象。<br>
* 此类的StackTraces和Messages是可利用的。<br>
*
* @author oldjiang
* @version 2012-01-17
* @see java.lang.Exception
* @since JDK 1.5
*/
public class ExceptionManager extends RuntimeException {

private static final long serialVersionUID = 6785236870166144787L;
private Throwable exception;
private String message;

/**
* 无参构造函数<br>
* 初始化Cause为null
*/
public ExceptionManager() {
initCause(null);
}

/**
* 带参构造函数,传入String参数
* @param msg String
*/
public ExceptionManager(String msg) {
super(msg);
initCause(null);
this.message=msg;
}

/**
* 带参构造函数,传入Throwable参数
* @param thrown Throwable
*/
public ExceptionManager(Throwable thrown) {
initCause(null);
exception = thrown;
}

/**
* 带参构造函数,传入String和Throwable参数
* @param msg String
* @param thrown Throwable
*/
public ExceptionManager(String msg, Throwable thrown) {
super(msg);
initCause(null);
this.message=msg;
this.exception = thrown;
}

/**
* 打印错误堆栈
*/
public void printStackTrace() {
printStackTrace(System.err);
}

/**
* 打印错误堆栈
* (字节打印流)
*/
public void printStackTrace(PrintStream outStream) {
printStackTrace(new PrintWriter(outStream));
}

/**
* 打印错误堆栈
* (字符打印流)
*/
public void printStackTrace(PrintWriter writer) {
super.printStackTrace(writer);
if (getException() != null) {
getException().printStackTrace(writer);
}
writer.flush();
}

/**
* exception's getter
* @return reception
*/
public Throwable getException() {
return exception;
}

/**
* @return exception
*/
public Throwable getCause() {
return exception;
}

/**
* message's getter
* @return message
*/
public String getMessage() {
return message;
}

/**
* message's setter
* @param message
*/
public void setMessage(String message) {
this.message = message;
}
}


自定义异常类完成后,还需要配置Struts2.xml文件,以便使你的自定义异常类生效。

<global-results>
<result name="allException">/WEB-INF/page/error.jsp</result> <!-- 自定义错误拦截器 -->
</global-results>

<!-- 自定义错误拦截器开始 -->
<global-exception-mappings>
<exception-mapping result="allException" exception="sigma.common.exception">
</exception-mapping>
</global-exception-mappings>
<!-- 自定义错误拦截器结束 -->


最后就是配置文件中错误跳转页面。

我的配置是/WEB-INF/page/error.jsp,看一下我的JSP代码。

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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>发生错误</title>
</head>
<body>
发生错误:<br/>
<s:property value="exception.message"/><br>
请点击此处<s:a href="javascript:window.history.back(-1);">返回上页</s:a>
</body>
</html>


注意:我使用了Struts2标签,若不适用标签,可使用EL表达式获取错误信息,如下:

${exception.message}


那么如何在Action中让这个自定义的异常拦截器打印出一个Hello world呢?

Action中的部分代码:

public String execute(){
throw new ExceptionManager("Hello world!");
}


为新人领悟自定义异常拦截器而写,代码拙劣,高手勿喷。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: