`
pavel
  • 浏览: 918352 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

统一异常处理

阅读更多

第一步:自定义异常类:

package com.zhongbin.oa.exception;
/*
 * 自定义异常类,用来处理系统的异常
 */
public class SystemException extends RuntimeException {
 //对应messageResurser的KEY ,自定义的ExceptionHandler用此

KEY获取异常消息
 private String key;
 //异常消息的的参数
 private Object[] values;
 
 public String getKey() {
  return key;
 }

 public Object[] getValues() {
  return values;
 }

 public SystemException() {
  super();
 }

 public SystemException(String message, Throwable

throwable) {
  super(message, throwable);
 }

 public SystemException(String message) {
  super(message);
 }

 public SystemException(Throwable throwable) {
  super(throwable);
 }
 
 /*
  *@param message  异常消息
  *@param key messageResurser的KEY
  */
 public SystemException(String message,String key){
  super(message);
  this.key = key;
 }
 
 /*
  *@param message  异常消息
  *@param key messageResurser的KEY
  *@param values key对应异常消息的的单个参数
  */
 public SystemException(String message,String key,Object

value){
  super(message);
  this.key = key;
  this.values = new Object[]{value};
 }
 /*
  *@param message  异常消息
  *@param key messageResurser的KEY
  *@param values key对应异常消息的的参数数组
  */
 public SystemException(String message,String key,Object[]

values){
  super(message);
  this.key = key;
  this.values =  values;
 }


}


第二步:自定义异常处理器

package com.zhongbin.oa.exception;

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 {


 /*
  *  @param ex 异常发生时传递过来的异常实例
  * @param ae struts-config.xml 文件中的异常信息的配置
  * @param mapping  struts-config.xml文件中的mapping信

息配置
  * @param formInstance  formBean
  * @param request request
  * @param response response
  */
 @Override
 public ActionForward execute(Exception ex, ExceptionConfig

ae,
   ActionMapping mapping, ActionForm.

formInstance,
   HttpServletRequest request,

HttpServletResponse response)
   throws ServletException {
  
  //构建一个返回路径
  ActionForward forward = null;
  
   if(ae.getPath() != null){
    forward = new ActionForward(ae.getPath

());
   }else {
    forward = mapping.getInputForward();
   }
  
   //只处理自定义的SystemException异常类型
    if (ex instanceof SystemException) {
   SystemException se = (SystemException) ex;
   
   String key = se.getKey();
   ActionMessage error = null;
   
   //判断有没有KEY
   if(key == null){
    key = ae.getKey();
    error = new ActionMessage

(key,ex.getMessage());
   }else{
    //判断有没有values
    if(se.getValues() != null){
     error = new ActionMessage

(key,se.getValues());
    }else{
     error = new ActionMessage

(key);
    }
   }
   //调用父类方法设置ERROR 在 SCOPE 中
   super.storeException(request, key, error,

forward, ae.getScope());
   return forward;
  } 
  
   //其余的仍交给父类处理
  return super.execute(ex, ae, mapping,

formInstance, request, response);
 }

}


第三步:设置status-config.xml文件
<global-exceptions>
  <exception
  key="error.detail"
  type="com.zhongbin.oa.exception.SystemException"
  scope="request"
  

handler="com.zhongbin.oa.exception.SystemExceptionHandler"
  path="/common/exception.jsp">
  </exception>

第四步,设置MessageResources.properties文件key的值
exception.del.org.001 = don't delete orgnization ,id is {0}
error.detail=error:{0}

第五步:在可能有误的地方抛出自定义的异常:

if(org.getChildren().size() > 0 ){
   throw new SystemException("不能删除,存在子机构","exception.del.org.001",""+org.getId());
  }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics