java web异常处理方式总结
在网上找了一下午的资料,终于清楚地了解在struts2+hibernate3+spring3下比较完善的异常处理方式,有借鉴的东西,也有些是自己修改后的东东,废话不多说了,看下图:
需要的东东有:Action类、service类、异常拦截器ExceptionFilter、自定义异常类MyException、错误跳转页面error.jsp
首先,Action类
/**
* 查询机票
* @return
* @throws MyException
* @throws Exception
*/
public String searchTicket() throws MyException
{
try {
baseService.findFlightInfos(searchTicketDto);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new MyException(101);// 抛出自定义异常
}
return "success";
}
以上baseService.findFlightInfos方法的关键代码
public List<FlightInfoDto> findFlightInfos(FlightInfoDto dto) {
// TODO Auto-generated method stub
int i = 5/0;// 此条语句作为测试
try{
// 数据库操作或一般操作
} catch (Exception e) {
// TODO Auto-generated catch block
}
return result;
}
接下来,异常拦截器ExceptionFilter
/**
*
*/
package com.ab.permission.Filter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.springframework.dao.DataAccessException;
import com.ab.permission.exception.MyException;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.StaticParametersInterceptor;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;
/**
* 功能描述:异常拦截器
*
* @author: LRR
* @date: 2014-04-07 下午05:09:25
*/
public class ExceptionFilter extends AbstractInterceptor {
private HttpServletRequest request;
private HttpSession session;
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* 功能描述:异常拦截器,对异常进行相关处理
*/
@Override
public String intercept(ActionInvocation actioninvocation) throws Exception {
request = ServletActionContext.getRequest();
String result = null; // Action的返回值
String errorMsg = "未知错误!";
try {
// 运行被拦截的Action,期间如果发生异常会被catch住
result = actioninvocation.invoke();
return result;
}
catch (DataAccessException ex) {
errorMsg = "数据库操作失败";
} catch (NullPointerException ex) {
errorMsg = "调用了未经初始化的对象或者是不存在的对象";
} catch (IOException ex) {
errorMsg = "IO异常";
} catch (ClassNotFoundException ex) {
errorMsg = "指定的类不存在";
} catch (ArithmeticException ex) {
errorMsg = "数学运算异常";
} catch (ArrayIndexOutOfBoundsException ex) {
errorMsg = "数组下标越界";
} catch (IllegalArgumentException ex) {
errorMsg = "方法的参数错误";
} catch (ClassCastException ex) {
errorMsg = "类型强制转换错误";
} catch (SecurityException ex) {
errorMsg = "违背安全原则异常";
} catch (SQLException ex) {
errorMsg = "操作数据库异常";
} catch (NoSuchMethodError ex) {
errorMsg = "方法末找到异常";
} catch (InternalError ex) {
errorMsg = "Java虚拟机发生了内部错误";
} catch (InvocationTargetException ex) {
errorMsg = "程序内部错误,操作失败,";
} catch (MyException e) {
e.printStackTrace(); //开发时打印异常信息,方便调试
errorMsg = e.getMessage().trim();
} catch (Exception ex) {
errorMsg = "程序内部错误,操作失败,";
}
errorMsg = "错误信息:"+errorMsg;
/**
* 发送错误消息到页面
*/
request.setAttribute("tip", errorMsg);
return "error";
}
}
错误跳转页面error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>出错了</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${ tip } <br>
</body>
</html>
最后就是struts.xml了
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<!-- 定义全局异常映射 -->
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<interceptors>
<interceptor name="exceptionManager" class="com.ab.permission.Filter.ExceptionFilter"/>
<interceptor-stack name="authorityStack">
<interceptor-ref name="defaultStack" />
</interceptor-ref>
<interceptor-ref name="exceptionManager" />
</interceptor-stack>
</interceptors>
至此工作已做完,最后测试结果可自行测试。
还没有评论,来说两句吧...