【发布时间】:2013-03-30 14:39:12
【问题描述】:
我想知道处理异常的更好方法是什么。我的 Web 项目包含 Struts 2、Spring 和 Hibernate。
异常可能发生在 Struts Action 或业务层或数据层。
如何处理每一层的异常?
【问题讨论】:
-
异常处理没有通用的神奇解决方案。
标签: spring hibernate exception struts2
我想知道处理异常的更好方法是什么。我的 Web 项目包含 Struts 2、Spring 和 Hibernate。
异常可能发生在 Struts Action 或业务层或数据层。
如何处理每一层的异常?
【问题讨论】:
标签: spring hibernate exception struts2
在 Struts 中,通过定义一个包含全局异常处理的抽象默认包来处理您可能委托给框架的异常(如果您不想手动处理)。
并在发生此异常时重定向到error_page.jsp。
你可以在Exception handling in Struts 2 and Hibernate找到更详细的描述:
在正常情况下,应捕获、记录并重新抛出异常。但是在 Struts2 中,您可以通过创建默认的应用程序拦截器堆栈来处理未捕获的异常
<interceptor-stack name="appDefaultStack"> <interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">ERROR</param> </interceptor-ref> </interceptor-stack>任何 此应用程序未捕获的异常将被记录并处理 通过全局异常映射
<global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="error"/> </global-exception-mappings> > <global-results> <result name="error">/error_page.jsp</result> </global-results>
见How to pass exceptions globally to a single action from other actions in Struts 2。
【讨论】: