struts2 环境下 异常设计 和处理

抽象出两种异常:
1.严重的不可恢复的异常:SystemException extends RuntimeException
2.有机会被处理的异常:AppException extends Exception

我的设计思路:在业务代码里面用MessageId等信息构造AppException或SystemException 并抛出,然后由拦截器转发到专用的异常处理Action中处理(如AppExceptionHandleAction或SystemExceptionHandleAction)。

我的问题是:在AppExceptionHandleAction或SystemExceptionHandleAction中,我怎样能取得那个AppException或SystemException 呢?之所以要取得这个异常对象,是想取得它的MessageId并进行日志和别的处理。

查了一下文档,actionSupport类里并没有相关的取得exception的方法。把异常转到Action中处理的思路有问题么?
请问各位达人们都是怎么做的?有没有什么最佳实践?

十分感谢!

框架:struts2.0.14,spring2.5.6,ibatis2.3.4

1个回答

用struts2的ExceptionMappingInterceptor
[code="java"]

package com.erry.core.web.interceptor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.springframework.dao.DataAccessException;

import com.erry.exception.BusinessException;
import com.erry.exception.DaExceptionTranslator;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.ExceptionHolder;
import com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor;
import com.opensymphony.xwork2.util.LocalizedTextUtil;

public class ExceptionInterceptor extends ExceptionMappingInterceptor {

/**
 *
 */
private static final long serialVersionUID = -108722874114862093L;
private DaExceptionTranslator daExTranslator = new DaExceptionTranslator();

private boolean debug = false;

protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {
    String detailedInfo;
    List<String> errMessage = new ArrayList<String>();
    //      try {
    detailedInfo = exceptionHolder.getExceptionStack();
    //      } catch (IOException e) {
    //          detailedInfo = "";
    //      }
    if (exceptionHolder.getException() instanceof BusinessException) {
        errMessage = getBusiExceptionMessage(invocation, (BusinessException) exceptionHolder.getException());
    } else if (exceptionHolder.getException() instanceof DataAccessException) {
        BusinessException e = daExTranslator.tranlate((DataAccessException) exceptionHolder.getException());
        errMessage.addAll(getBusiExceptionMessage(invocation, e));
    } else {
        errMessage.add(getMessage(invocation, "unknown_error", null));
    }
    invocation.getStack().set("detailedInfo", detailedInfo);
    invocation.getStack().set("errMessage", errMessage);
    if (debug) {
        exceptionHolder.getException().printStackTrace();
    }
}

private List<String> getBusiExceptionMessage(ActionInvocation invocation, BusinessException e) {
    List<String> errors = new ArrayList<String>();
    BusinessException be = e;
    while (be != null) {
        String msgKey = "bussiness_exception_" + be.getErrorCode();
        errors.add(getMessage(invocation, msgKey, be.getArgs()));
        be = be.getLinkedException();
    }

    return errors;
}

private String getMessage(ActionInvocation invocation, String msgKey, Object[] args) {
    Locale locale = invocation.getInvocationContext().getLocale();
    return LocalizedTextUtil.findText(invocation.getAction().getClass(), msgKey, locale, msgKey, args);
}

public boolean isDebug() {
    return debug;
}

public void setDebug(boolean debug) {
    this.debug = debug;
}

}

[/code]

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问