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";
}
最佳答案
有几种方法可以做到这一点。
如果可以的话,最好的方法是实现/覆盖 MyBusinessException 的 toString() 方法。每个异常都有一个 toString() 方法,因此无论您在 showError 方法中得到什么 FlowExecutionException,您仍然会在字符串中获得有意义的错误描述。
public String showError(FlowExecutionException flowExecutionException) {
return flowExecutionException.toString();
}
后备方法,例如如果您无权访问 MyBusinessException 内部的代码,则可以使用 instanceof 运算符来确定 MyBusinessException 是否是 FlowExecutionException 的有效类、父类(super class)、接口(interface)或超接口(interface)。
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();
}
关于java - Spring Webflow中如何将异常传递给方法的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8431520/