Struts2自定义异常拦截器
2012-01-17 15:19
405 查看
首先自定一个ExceptionManager类,该类继承自RuntimeException,属于Unchecked类型异常。
自定义异常类完成后,还需要配置Struts2.xml文件,以便使你的自定义异常类生效。
最后就是配置文件中错误跳转页面。
我的配置是/WEB-INF/page/error.jsp,看一下我的JSP代码。
注意:我使用了Struts2标签,若不适用标签,可使用EL表达式获取错误信息,如下:
那么如何在Action中让这个自定义的异常拦截器打印出一个Hello world呢?
Action中的部分代码:
为新人领悟自定义异常拦截器而写,代码拙劣,高手勿喷。
/* * 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!"); }
为新人领悟自定义异常拦截器而写,代码拙劣,高手勿喷。
相关文章推荐
- Struts2的异常处理机制、自定义拦截器
- Struts2 自定义异常拦截器
- springMVC高级部分(数据校验,数据错误回显(自定义格式错误显示),拦截器,异常处理,文件上传,文件下载,springmvc运行流程以及springmvc和struts2对比)
- struts2声明式异常处理,友好的错误界面,自定义异常拦截器,
- Struts2的异常处理机制、自定义拦截器
- Struts2重新学习之自定义拦截器(判断用户是否是登录状态)
- struts2中拦截器的全局拦截和自定义拦截
- Struts2自定义拦截器
- struts2中关于使用自定义的拦截器后,从页面提交过来的bean的参数的值为null的误区及解决
- Struts2自定义拦截器:登录拦截
- struts2多个自定义拦截器的栈
- Struts2自定义拦截器实例—登陆权限验证
- Struts2自定义拦截器的实现
- struts2的拦截器自定义,使用默认拦截器,组合拦截器堆 的案例
- Struts2自定义拦截器
- struts2自定义登录拦截器
- struts2-自定义拦截器
- 初学Struts2-自定义拦截器及其配置
- 自定义Struts2的拦截器&自定义UserAware接口&Action中模型赋值问题&Hibernate懒加载问题
- Struts2自定义拦截器和检验器