Struts2自定义异常拦截器

首先自定一个ExceptionManager类,该类继承自RuntimeException,属于Unchecked类型异常。

  1 /*
2 * file name:ExceptionManager.java
3 * package:sigma.common.exception
4 * class name:ExceptionManager
5 * create time:2012-01-17
6 * Copyright: Copyright (c) 2012
7 */
8
9 package sigma.common.exception;
10
11 import java.io.PrintStream;
12 import java.io.PrintWriter;
13
14 /**
15 * 自定义异常类<br>
16 * 这个<code>ExceptionManager</code>类继承自<code>RuntimeException</code>,属于unchecked类型异常。<br>
17 * 此类用于统一打包所有的<code>Throwable</code>实例和对象。<br>
18 * 此类的StackTraces和Messages是可利用的。<br>
19 *
20 * @author oldjiang
21 * @version 2012-01-17
22 * @see java.lang.Exception
23 * @since JDK 1.5
24 */
25 public class ExceptionManager extends RuntimeException {
26
27 private static final long serialVersionUID = 6785236870166144787L;
28 private Throwable exception;
29 private String message;
30
31 /**
32 * 无参构造函数<br>
33 * 初始化Cause为null
34 */
35 public ExceptionManager() {
36 initCause(null);
37 }
38
39 /**
40 * 带参构造函数,传入String参数
41 * @param msg String
42 */
43 public ExceptionManager(String msg) {
44 super(msg);
45 initCause(null);
46 this.message=msg;
47 }
48
49 /**
50 * 带参构造函数,传入Throwable参数
51 * @param thrown Throwable
52 */
53 public ExceptionManager(Throwable thrown) {
54 initCause(null);
55 exception = thrown;
56 }
57
58 /**
59 * 带参构造函数,传入String和Throwable参数
60 * @param msg String
61 * @param thrown Throwable
62 */
63 public ExceptionManager(String msg, Throwable thrown) {
64 super(msg);
65 initCause(null);
66 this.message=msg;
67 this.exception = thrown;
68 }
69
70 /**
71 * 打印错误堆栈
72 */
73 public void printStackTrace() {
74 printStackTrace(System.err);
75 }
76
77 /**
78 * 打印错误堆栈
79 * (字节打印流)
80 */
81 public void printStackTrace(PrintStream outStream) {
82 printStackTrace(new PrintWriter(outStream));
83 }
84
85 /**
86 * 打印错误堆栈
87 * (字符打印流)
88 */
89 public void printStackTrace(PrintWriter writer) {
90 super.printStackTrace(writer);
91 if (getException() != null) {
92 getException().printStackTrace(writer);
93 }
94 writer.flush();
95 }
96
97 /**
98 * exception's getter
99 * @return reception
100 */
101 public Throwable getException() {
102 return exception;
103 }
104
105 /**
106 * @return exception
107 */
108 public Throwable getCause() {
109 return exception;
110 }
111
112 /**
113 * message's getter
114 * @return message
115 */
116 public String getMessage() {
117 return message;
118 }
119
120 /**
121 * message's setter
122 * @param message
123 */
124 public void setMessage(String message) {
125 this.message = message;
126 }
127 }

 

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

 1     <global-results>
2 <result name="allException">/WEB-INF/page/error.jsp</result> <!-- 自定义错误拦截器 -->
3 </global-results>
4
5 <!-- 自定义错误拦截器开始 -->
6 <global-exception-mappings>
7 <exception-mapping result="allException" exception="sigma.common.exception">
8 </exception-mapping>
9 </global-exception-mappings>
10 <!-- 自定义错误拦截器结束 -->

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

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

 1 <%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
2 <%@ taglib uri="/struts-tags" prefix="s" %>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
7 <title>发生错误</title>
8 </head>
9 <body>
10 发生错误:<br/>
11 <s:property value="exception.message"/><br>
12 请点击此处<s:a href="javascript:window.history.back(-1);">返回上页</s:a>
13 </body>
14 </html>

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

1 ${exception.message}

 

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

Action中的部分代码:

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



为新人领悟自定义异常拦截器而写,代码拙劣,高手勿喷。

posted on 2012-01-17 15:19  oldjiang  阅读(2981)  评论(1编辑  收藏  举报

导航