Struts 2和Hibernate中的异常处理 [英] Exception handling in Struts 2 and Hibernate

查看:137
本文介绍了Struts 2和Hibernate中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们在Struts2,Hibernate,MySQL中开发了一个网站,并且我们添加了几个 try / catch 块,这里是通过Hibernate封装数据库调用的。我的问题是


  1. 在catch块内,我发送适当的消息给记录器。在这里,我们不能使用 System.out.println 作为其网页,还可以做什么来提醒用户有关异常?


  2. 作为测试的一部分,我更改了 hibernate.cfg.xml 并输入错误的数据库密码,以模拟数据库崩溃情况。 / p>


正如我所料,它抛出错误

  javax.servlet.ServletException:过滤器执行抛出一个异常

java.lang.NoClassDefFoundError:com_cenqua_clover / CoverageRecorder
my.com.employee。< init> .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.ne wInstance(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.DefaultActionProx yFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher。 java:395)
注意Apache Tomcat / 7.0.20日志中提供了根本原因的完整堆栈跟踪。

从用户的角度来看,这是不理想的,所以如何解决这些问题。我正在使用Eclipse Juno,Windows XP,MySQL 5.5。

解决方案

异常是程序或开发人员无法处理的结果情况异常行为。在这种情况下,开发人员可以知道异常被抛出,可能会采取一些行动来解决这个问题。



普通用户不需要知道任何异常,而是系统管理员实际做的。因此,记录异常很有可能进一步解决任何问题。



这在开发阶段也很有用,当开发人员需要通过打印stacktrace调试问题,重新抛出异常。在这种情况下很少适用于捕捉和忽视例外。



在正常情况下,异常应该被捕获,记录并重新引导。但是在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-映射> 
< exception-mapping exception =java.lang.Exceptionresult =error/>
< / global-exception-mappings>

< global-results>
< result name =error> /error_page.jsp< / result>
< / global-results>


Suppose that we have developed a website in Struts2, Hibernate, MySQL and we have added few try/catch blocks here are there which encloses database calls via Hibernate. My question is

  1. Inside the catch block i am sending appropriate message to a logger. Here we can't use System.out.println as its a webpage, what else can be done to alert the user about exception?

  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.

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.

解决方案

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

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

这篇关于Struts 2和Hibernate中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆