简体   繁体   English

Struts 2 和 Hibernate 中的异常处理

[英]Exception handling in Struts 2 and Hibernate

Suppose that we have developed a website in Struts 2, Hibernate, MySQL and we have added few try/catch blocks here are there which encloses database calls via Hibernate.假设我们已经在 Struts 2、Hibernate、MySQL 中开发了一个网站,并且我们在这里添加了一些try/catch块,它们通过 Hibernate 封装了数据库调用。

My question is:我的问题是:

  1. Inside the catch block l am sending appropriate message to a logger.在 catch 块内,我正在向记录器发送适当的消息。 Here we can't use System.out.println as its a webpage, what else can be done to alert the user about exception?这里我们不能使用System.out.println作为它的网页,还有什么可以做来提醒用户异常?

  2. As a part of testing I changed the hibernate.cfg.xml and input wrong database password so as to simulate the database crash scenario.作为测试的一部分,我更改了hibernate.cfg.xml并输入了错误的数据库密码,以模拟数据库崩溃的场景。

as I expected It threw error:正如我所料它抛出错误:

    javax.servlet.ServletException: Filter execution threw an exception

    java.lang.NoClassDefFoundError: com_cenqua_clover/CoverageRecorder
    my.com.employee.<init>(employee.java:29)
    com.action.employeeAction.<init>(employeeAction.java:23)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    java.lang.Class.newInstance0(Class.java:355)
    java.lang.Class.newInstance(Class.java:308)
    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:119)
    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:150)
    com.opensymphony.xwork2.ObjectFactory.buildBean(ObjectFactory.java:139)
    com.opensymphony.xwork2.ObjectFactory.buildAction(ObjectFactory.java:109)
    com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:288)
    com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
    note The full stack trace of the root cause is available in the Apache Tomcat/7.0.20 logs. 

and from users perspective this is not desirable right, so how to tackle such problems.而从用户的角度来看,这是不可取的,那么如何解决此类问题。 I am using Eclipse Juno, Windows XP, MySQL 5.5.我使用的是 Eclipse Juno、Windows XP、MySQL 5.5。

Exceptions are the results of the program or developer can't handle the situation with abnormal behavior.异常是程序或开发人员无法处理异常行为的情况的结果。 In this case the developer could know that the exception is thrown and could make some actions toward resolving the case.在这种情况下,开发人员可以知道抛出了异常,并且可以采取一些措施来解决该情况。

The ordinary user doesn't need to know about any exception but the system administrators actually do.普通用户不需要知道任何异常,但系统管理员实际上知道。 So, logging exceptions are good making the possibility to solve any problems further.因此,记录异常是很好的,可以进一步解决任何问题。

This is also useful on the development stage, when the developer needs to debug the issue via printing stacktrace, rethrowing exception.这在开发阶段也很有用,当开发人员需要通过打印堆栈跟踪来调试问题时,重新抛出异常。 Rarely applicable in this situation the catching and ignoring exceptions.在这种情况下很少适用捕获和忽略异常。

In the normal situation exceptions should be caught, logged and rethrown.在正常情况下,异常应该被捕获、记录和重新抛出。 But in the Struts2 you could handle uncaught exceptions via creating the default application interceptor stack但是在 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>

any exceptions not caught by this application will be logged and then handled by the global exception mapping此应用程序未捕获的任何异常都将被记录下来,然后由全局异常映射处理

<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>

The user doesn't need to know there was a database exception, the developer does.用户不需要知道有数据库异常,开发人员知道。

The specific exception should be logged.应该记录特定的异常。 The user should see that an operation failed.用户应该看到操作失败。 Generally you'd either simply report this through normal S2 means, eg, an error message.通常,您要么通过正常的 S2 方式简单地报告此问题,例如,错误消息。

You may also throw an application-specific exception and use S2's declarative exception handling.您还可以抛出特定于应用程序的异常并使用 S2 的声明性异常处理。

You may use declarative exception handling to handle any or all low-level exceptions, but IMO that's either too general, or too tedious.可以使用声明性异常处理来处理任何或所有低级异常,但 IMO 要么太笼统,要么太乏味。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM