【问题标题】:How to handle exceptions in a project using Struts 2, Spring, and Hibernate如何使用 Struts 2、Spring 和 Hibernate 处理项目中的异常
【发布时间】:2013-03-30 14:39:12
【问题描述】:

我想知道处理异常的更好方法是什么。我的 Web 项目包含 Struts 2、Spring 和 Hibernate。

异常可能发生在 Struts Action 或业务层或数据层。

如何处理每一层的异常?

【问题讨论】:

标签: spring hibernate exception struts2


【解决方案1】:

在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-17
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多