如何将异常传递给Spring Webflow中方法的方法?

时间:2011-12-08 13:12:03

标签: java spring-webflow

Spring WebFlow 3中传输异常的问题

预定义方法抛出如下的异常:

public class MyBusinessException extends BusinessException {

    private static final long serialVersionUID = -1276359772397342392L;

    private Long min = null;

    private Long max = null;

    public static final String CODE_1 = "code.1.business.exception";

    public static final String CODE_2 = "code.2.business.exception";

    public MyBusinessException (String code, Exception ex) {
        super(code, ex);
    }

    public MyBusinessException (String code) {
        super(code);
    }

    public MyBusinessException (Long min, Long max, final String messageCode) {
        super(messageCode);
        this.min = min;
        this.max = max;

    }

    public Long getMin() {
        return min;
    }

    public Long getMax() {
        return max;
    }

}

在webflow中捕获异常后的转换看起来像这样

<transition on-exception="MyBusinessException" to="start" >
            <evaluate expression="actionService.showError(flowExecutionException)" result="flashScope.refreshError"/>
</transition>

在操作中,showError将从异常和最小值和最大值中检索消息。怎么做。请帮忙。

public String showError(FlowExecutionException flowExecutionException) {
        flowExecutionException.?
        return "someString";
    }

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。

最佳方式(如果可以的话)是实现/覆盖MyBusinessException的toString()方法。每个Exception都有一个toString()方法,所以无论你在showError方法中得到什么样的FlowExecutionException,你仍然会在String中得到一个有意义的错误描述。

public String showError(FlowExecutionException flowExecutionException) {
    return flowExecutionException.toString();
}

后备方法,例如如果您无权访问MyBusinessException中的代码,则可以使用instanceof运算符来确定MyBusinessException是否是FlowExecutionException的有效类,超类,接口或超接口。

public String showError(FlowExecutionException flowExecutionException) {
    if (flowExecutionException instanceof MyBusinessException) {
        MyBusinessException usableException = (MyBusinessException)flowExecutionException;

        int min = usableException.getMin();
        int max = usableException.getMax();

        return "MyBusinessException min="+min+", max="+max;

    } else return flowExecutionException.toString();
}