king9577 2011-07-29 16:21
浏览 223
已采纳

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条回答 默认 最新

  • chenli348105874 2011-07-29 17:05
    关注

    用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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗