struts2 如何将拦截器里产生的异常信息输入到统一的错误页面中?

 我来答
修行鱼
2012-05-02 · TA获得超过1163个赞
知道答主
回答量:100
采纳率:66%
帮助的人:13.4万
展开全部
利用struts2的异常处理机制和拦截器机制可以很方便的实现异常处理功能,你不再需要在Action中捕获异常,并抛出相关的异常了,这些都交给拦截器来帮你做了。
如果在Action中使用了try{}catch{}处理异常,异常信息直接打印到tomcat控制台,如果是抛出的话,控制台看不到信息,但又想看异常信息,以便排错,可以使用如下方法:
Struts2 XML:
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
<result name="exception">error.jsp</result>

Jsp:
异常信息 :<s:property value="exception.message" />
异常值栈:<s:property value="exceptionStack" />

在拦截器中,捕获常见的异常,并以友好异常信息抛出,如下:
public class BusinessException extends RuntimeException{
private static final long serialVersionUID = 0xc1a865c45ffdc5f9L;
public BusinessException(String frdMessage){
super(createFriendlyErrMsg(frdMessage));
}
public BusinessException(Throwable throwable){
super(throwable);
}
public BusinessException(Throwable throwable, String frdMessage){
super(throwable);
}
private static String createFriendlyErrMsg(String msgBody) {
String prefixStr = "抱歉,";
String suffixStr = " 请稍后再试或与管理员联系!";
StringBuffer friendlyErrMsg = new StringBuffer("");
friendlyErrMsg.append(prefixStr);
friendlyErrMsg.append(msgBody);
friendlyErrMsg.append(suffixStr);
return friendlyErrMsg.toString();
}

}

public class BusinessInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
before(invocation);
String result = "";
try {
result = invocation.invoke();
} catch (DataAccessException ex) {
throw new BusinessException("数据库操作失败!");
} catch (NullPointerException ex) {
throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");
} catch (IOException ex) {
throw new BusinessException("IO异常!");
} catch (ClassNotFoundException ex) {
throw new BusinessException("指定的类不存在!");
} catch (ArithmeticException ex) {
throw new BusinessException("数学运算异常!");
} catch (ArrayIndexOutOfBoundsException ex) {
throw new BusinessException("数组下标越界!");
} catch (IllegalArgumentException ex) {
throw new BusinessException("方法的参数错误!");
} catch (ClassCastException ex) {
throw new BusinessException("类型强制转换错误!");
} catch (SecurityException ex) {
throw new BusinessException("违背安全原则异常!");
} catch (SQLException ex) {
throw new BusinessException("操作数据库异常!");
} catch (NoSuchMethodError ex) {
throw new BusinessException("方法末找到异常!");
} catch (InternalError ex) {
throw new BusinessException("Java虚拟机发生了内部错误");
} catch (Exception ex) {
throw new BusinessException("程序内部错误,操作失败!");
}
after(invocation, result);
return result;
}
}

XML配置:
<!--自定义拦截器 -->
<interceptors>
<interceptor name="businessInterceptor" class="org.test.BusinessInterceptor"></interceptor>
</interceptors>
<action name="test" class="org.test.action.BusinessAction">
<result name="success">test.jsp</result>
<exception-mapping result="exception" exception="java.lang.Exception"></exception-mapping>
<result name="exception">error.jsp</result>
<!-- 拦截器一般配在result之后 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="businessInterceptor"></interceptor-ref>
</action>

如果配合Log4j的话,还可以实现将异常信息输出到文件以及控制台。
参考:http://blog.csdn.net/colin_guo/article/details/6932493
072jncgnt
推荐于2016-10-22 · TA获得超过2310个赞
知道大有可为答主
回答量:4534
采纳率:0%
帮助的人:824万
展开全部
首先,你的拦截器应该有一个返回页面,也就是说触发拦截时要返回到的页面。(比方说登陆画面)这个你可以自己在配置文件里定义,你应该用过struts2的校验功能吧,如果用过的话,应该就知道如何在页面显示提示信息了。(针对struts2的重写方法校验,不是XML校验框架)
简单来说就是:
action:
addFieldError方法

jsp:
<s:fielderror></s:fielderror>

当然,显示提示信息可以灵活运用,你完全可以把信息设置在自己定义的控件上。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式