天天看点

【Struts2框架】第五节声明式异常处理-处理异常的过程

如何抓到异常的?

步骤:(以之前写的小样例为例)

如果categoryservice中的list方法出错(category_表不存在):

做到两点:throws sqlexception和throw(e);

此时categoryaction接收到categoryservice中的list方法抛出的异常:

注意加:throws exception

categoryaction中的throws exception继续向外抛异常,之后struts.xml接住

其中 <exception-mapping result="error" exception="java.sql.sqlexception"/>  

java.sql.sqlexception异常有对应的result,找到result(error.jsp),显示异常的界面

在error.jsp中利用[debug]可以看到,栈值中有exception,详细为:

exception   java.sql.sqlexception: table 'bbs2014.category_' doesn't exist

友好界面里最好写“出错了,请联系管理员”,要比“请稍后再试”好多了

全局的异常处理:

如果需要收集的异常过多,那么需要全局的异常处理机制,在struts中这样配置:

struts中支持声明式的异常处理指的是,要是有异常,就向外抛,最后会给一个统一的接口,然后让你在特定的页面做出处理。

转载请注明出处:http://blog.csdn.net/acmman/article/details/47086261