Struts 2 和 Hibernate 中的异常处理
假设我们在 Struts 2、Hibernate、MySQL 中开发了一个网站,并且我们在此处添加了一些 try/catch
块,这些块包含通过 Hibernate 进行的数据库调用.
我的问题是:
在 catch 块中,我正在向记录器发送适当的消息.这里我们不能使用
System.out.println
作为网页,还有什么可以提醒用户异常?作为测试的一部分,我更改了
hibernate.cfg.xml
并输入了错误的数据库密码,以模拟数据库崩溃的场景.
正如我所料,它抛出了错误:
javax.servlet.ServletException: 过滤器执行抛出异常java.lang.NoClassDefFoundError: com_cenqua_clover/CoverageRecordermy.com.employee.<init>(employee.java:29)com.action.employeeAction.<init>(employeeAction.java:23)sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)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 Apache Tomcat/7.0.20 日志中提供了根本原因的完整堆栈跟踪.
从用户的角度来看,这是不可取的权利,那么如何解决这些问题.我正在使用 Eclipse Juno、Windows XP、MySQL 5.5.
解决方案异常是程序或开发人员无法处理异常行为的情况.在这种情况下,开发人员可以知道引发了异常,并可以采取一些措施来解决问题.
普通用户不需要知道任何异常,但系统管理员确实知道.因此,记录异常有助于进一步解决任何问题.
这在开发阶段也很有用,当开发人员需要通过打印堆栈跟踪来调试问题时,重新抛出异常.在这种情况下很少适用于捕获和忽略异常.
在正常情况下,应捕获、记录并重新抛出异常.但是在 Struts2 中,您可以通过创建默认的应用程序拦截器堆栈来处理未捕获的异常
<interceptor-stack name="appDefaultStack"><拦截器引用名称=defaultStack"><param name="exception.logEnabled">true</param><param name="exception.logLevel">ERROR</param></拦截器引用></拦截器堆栈>
<块引用>
任何此应用程序未捕获的异常将被记录并处理通过全局异常映射
<global-exception-mappings><异常映射异常=java.lang.Exception";结果=错误"/></全局异常映射><全局结果><结果名称=错误">/error_page.jsp</result></全球结果>
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.
My question is:
Inside the catch block l 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?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>
相关文章