<i id='h5rus'><tr id='h5rus'><dt id='h5rus'><q id='h5rus'><span id='h5rus'><b id='h5rus'><form id='h5rus'><ins id='h5rus'></ins><ul id='h5rus'></ul><sub id='h5rus'></sub></form><legend id='h5rus'></legend><bdo id='h5rus'><pre id='h5rus'><center id='h5rus'></center></pre></bdo></b><th id='h5rus'></th></span></q></dt></tr></i><div id='h5rus'><tfoot id='h5rus'></tfoot><dl id='h5rus'><fieldset id='h5rus'></fieldset></dl></div>

    • <bdo id='h5rus'></bdo><ul id='h5rus'></ul>

  • <tfoot id='h5rus'></tfoot>

    <legend id='h5rus'><style id='h5rus'><dir id='h5rus'><q id='h5rus'></q></dir></style></legend>

      <small id='h5rus'></small><noframes id='h5rus'>

      1. Struts 2 和 Hibernate 中的异常处理

        Exception handling in Struts 2 and Hibernate(Struts 2 和 Hibernate 中的异常处理)
      2. <legend id='MQb9m'><style id='MQb9m'><dir id='MQb9m'><q id='MQb9m'></q></dir></style></legend>

      3. <small id='MQb9m'></small><noframes id='MQb9m'>

            <i id='MQb9m'><tr id='MQb9m'><dt id='MQb9m'><q id='MQb9m'><span id='MQb9m'><b id='MQb9m'><form id='MQb9m'><ins id='MQb9m'></ins><ul id='MQb9m'></ul><sub id='MQb9m'></sub></form><legend id='MQb9m'></legend><bdo id='MQb9m'><pre id='MQb9m'><center id='MQb9m'></center></pre></bdo></b><th id='MQb9m'></th></span></q></dt></tr></i><div id='MQb9m'><tfoot id='MQb9m'></tfoot><dl id='MQb9m'><fieldset id='MQb9m'></fieldset></dl></div>

                  <tbody id='MQb9m'></tbody>
                <tfoot id='MQb9m'></tfoot>

                • <bdo id='MQb9m'></bdo><ul id='MQb9m'></ul>
                  本文介绍了Struts 2 和 Hibernate 中的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我们在 Struts 2、Hibernate、MySQL 中开发了一个网站,并且我们在此处添加了一些 try/catch 块,这些块包含通过 Hibernate 进行的数据库调用.

                  我的问题是:

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

                  2. 作为测试的一部分,我更改了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:

                  1. 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?

                  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 中的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)
                  <legend id='ShHU1'><style id='ShHU1'><dir id='ShHU1'><q id='ShHU1'></q></dir></style></legend>
                  • <bdo id='ShHU1'></bdo><ul id='ShHU1'></ul>

                    • <tfoot id='ShHU1'></tfoot>

                        1. <small id='ShHU1'></small><noframes id='ShHU1'>

                          <i id='ShHU1'><tr id='ShHU1'><dt id='ShHU1'><q id='ShHU1'><span id='ShHU1'><b id='ShHU1'><form id='ShHU1'><ins id='ShHU1'></ins><ul id='ShHU1'></ul><sub id='ShHU1'></sub></form><legend id='ShHU1'></legend><bdo id='ShHU1'><pre id='ShHU1'><center id='ShHU1'></center></pre></bdo></b><th id='ShHU1'></th></span></q></dt></tr></i><div id='ShHU1'><tfoot id='ShHU1'></tfoot><dl id='ShHU1'><fieldset id='ShHU1'></fieldset></dl></div>
                            <tbody id='ShHU1'></tbody>