上一篇文章曾说到自动式异常处理,但是,在项目开发的过程中,是不用的,哈哈
因为:
1. 多参时无法实现
2.要为不同类型的异常坐配置,处理量太大
在项目开发的过程中,一般是用一个统一的异常类来进行异常处理
1.定义统一的异常类,这类继承RuntimeException
2.定义相对应的Handler,它继承于ExceptionHandler
3.在sturuts-config.xml中的配置
做好以上三部后就可以使用自己所定义的SystemException,例如:
在做机构管理的时候,先判断是否存在子机构,如果存在子机构,就不给删除
xwj.oa.del:是所对应的国际化资源文件中的错误信息xwj.oa.del=can not del,have children!,id is {0}
new Integer(org.getId()):是国际化资源文件的错误信息的参数
因为:
1. 多参时无法实现
2.要为不同类型的异常坐配置,处理量太大
在项目开发的过程中,一般是用一个统一的异常类来进行异常处理
1.定义统一的异常类,这类继承RuntimeException
package com.xwj.oa.utils;
public class SystemException extends RuntimeException {
private String key;
private Object[] values;
public Object[] getValues() {
return values;
}
public String getKey() {
return key;
}
public SystemException() {
super();
}
public SystemException(String message, Throwable cause) {
super(message, cause);
}
public SystemException(String message) {
super(message);
}
public SystemException(Throwable cause) {
super(cause);
}
public SystemException(String message,String key) {
super(message);
this.key = key;
}
public SystemException(String message,String key,Object values) {
super(message);
this.key = key;
this.values = new Object[]{values};
}
public SystemException(String message,String key,Object[] values) {
super(message);
this.key = key;
this.values =values;
}
}
2.定义相对应的Handler,它继承于ExceptionHandler
package com.xwj.oa.utils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
public class SystemExceptionHandler extends ExceptionHandler {
public ActionForward execute(Exception ex, ExceptionConfig ae,
ActionMapping mapping, ActionForm formInstance,
HttpServletRequest request, HttpServletResponse response)
throws ServletException {
if (ex instanceof SystemException) {
SystemException se = (SystemException) ex;
String key = se.getKey();
ActionMessage error = null;
if (key == null) {
error = new ActionMessage(ae.getKey(), se.getMessage());
} else {
if(se.getValues() != null){
error = new ActionMessage(key,se.getValues());
}else{
error = new ActionMessage(key);
}
}
ActionForward forward = null;
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
this.storeException(request, key, error, forward, ae.getScope());
return forward;
}
return super.execute(ex, ae, mapping, formInstance, request, response);
}
}
3.在sturuts-config.xml中的配置
<global-exceptions>
<exception key="errors.detail" type="java.lang.Exception" path="/common/exception.jsp" scope="request" handler="com.xwj.oa.utils.SystemExceptionHandler"/>
</global-exceptions>
做好以上三部后就可以使用自己所定义的SystemException,例如:
在做机构管理的时候,先判断是否存在子机构,如果存在子机构,就不给删除
if(org.getChildren().size() > 0){
throw new SystemException("存在子机构,无发删除","xwj.oa.del",new Integer(org.getId()));
}
xwj.oa.del:是所对应的国际化资源文件中的错误信息xwj.oa.del=can not del,have children!,id is {0}
new Integer(org.getId()):是国际化资源文件的错误信息的参数