简体   繁体   English

JSF 2.1 + Spring + hibernate 如何正确实现异常处理?

[英]JSF 2.1 + Spring + hibernate how to Implement correctly Exception handling?

I'm working on project which has the following structure:我正在研究具有以下结构的项目:

DaoService - is Spring bean which has SessionFactory object and performs Database manipulation on Database using Hibernate. DaoService - 是 Spring bean,它具有 SessionFactory object 并使用 Hibernate 对数据库执行数据库操作。 marked with @Repository标有@Repository

several BLogicService services - are Spring beans which have DaoService Autowired and performs some operation on POJOs and persist it in Hibernate.几个 BLogicService 服务- 是 Spring bean,它们具有自动装配的 DaoService 并对 POJO 执行一些操作并将其保存在 Hibernate 中。 Marked with @Service annotations.标有@Service注释。

JSF 2.1 Managed Beans - iterate with XHTML pages and hold properties and JSF actions. JSF 2.1 托管 Bean - 使用 XHTML 页面迭代并保存属性和 JSF 操作。 Marked as @ManagedBean and receive BlLogicServices objects from Spring as @ManagedProperty标记为@ManagedBean并从 Spring 接收 BlLogicServices 对象为@ManagedProperty

and finally XHTML pages which access managed beans.最后是访问托管 bean 的 XHTML 页面。

What would be correct way to manage exception handling in such application?在此类应用程序中管理异常处理的正确方法是什么? If i have exception on DAO Level, what would be the correct way to forwatd it to GUI?如果我在 DAO 级别有异常,将它转换为 GUI 的正确方法是什么?

If I were working with Spring MVC I would use `@ExceptionHandler, but how can it be done in JSF 2.1 with Spring?如果我使用的是 Spring MVC,我会使用`@ExceptionHandler,但是如何在 JSF 2.1 中使用 Spring 来完成?

To create a general exception catcher for all unexpected exceptions during BL processing you can implement ExceptionHandlerFactory and specify it in the faces-config.xml:要在 BL 处理期间为所有意外异常创建通用异常捕获器,您可以实现 ExceptionHandlerFactory 并在 faces-config.xml 中指定它:

  <factory>
    <exception-handler-factory>
      my.package.ExceptionHandlerFactory
    </exception-handler-factory>
  </factory>

It should create ExceptionHandler implementation which in turn implements handle method for example one consuming exceptions (I think I have taken it from JSF2 reference or similar source):它应该创建 ExceptionHandler 实现,该实现反过来实现处理方法,例如一个消耗异常(我想我已经从 JSF2 参考或类似来源获取它):

  private static class MyExceptionHandler extends ExceptionHandlerWrapper {
    private ExceptionHandler parent;

    public WfExceptionHandler(ExceptionHandler parent) {
      this.parent = parent;
    }

    @Override
    public ExceptionHandler getWrapped() {
      return this.parent;
    }

    @Override
    public void handle() throws FacesException {
      for (Iterator<ExceptionQueuedEvent> i =
           getUnhandledExceptionQueuedEvents().iterator();
           i.hasNext();) {
        ExceptionQueuedEvent event = i.next();
        i.remove();
        ExceptionQueuedEventContext context =
          (ExceptionQueuedEventContext) event.getSource();
        Throwable t = context.getException();
        myProcessing(t);
      }
    }
    ...
  }

myProcessing(t) can retrieve managed bean which will print exception to your gui console or you can just use FacesContext.getCurrentInstance().addMessage(). myProcessing(t) 可以检索将向您的 gui 控制台打印异常的托管 bean,或者您可以只使用 FacesContext.getCurrentInstance().addMessage()。 You will also need to call FacesContext.getCurrentInstance().renderResponse() to update the view since unhandled exception aborted the JSF lifecycle.您还需要调用 FacesContext.getCurrentInstance().renderResponse() 来更新视图,因为未处理的异常中止了 JSF 生命周期。

Alternatively you can use try/catch in all your managed beans methods and execute equivalent of myProcessing(t) there.或者,您可以在所有托管 bean 方法中使用 try/catch 并在那里执行等效的 myProcessing(t)。 The difference is ExceptionHandler will also catch exceptions during page rendering, not necessarily generated by your classes.不同之处在于 ExceptionHandler 还会在页面呈现期间捕获异常,不一定由您的类生成。

Exceptions should not be passed to the UI.不应将异常传递给 UI。

The fact that you're using a web MVC layer says that you've got controllers of some kind to accept, validate, and bind incoming requests;您使用 web MVC 层这一事实表明您有某种控制器来接受、验证和绑定传入请求; choose a handler for fulfilling the request;选择一个处理程序来满足请求; package the response, good or bad; package的反应,好坏; and choose the next view.并选择下一个视图。 The exception needs to make its way back to that controller, which will then make the necessary information and choose the error view.异常需要返回到 controller,然后它将提供必要的信息并选择错误视图。

The rest of your question is just confusing the issue.您问题的 rest 只是混淆了这个问题。 DAOs, MDBs, etc. - all need to be marshalled by a handler of some kind. DAO、MDB 等 - 都需要由某种处理程序进行编组。 (Does JSF still call those Actions? I never use it.) That handler should catch any exceptions and communicate them back to the UI via the controller. (JSF 仍然调用那些动作吗?我从不使用它。)该处理程序应该捕获任何异常并通过 controller 将它们传达回 UI。

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

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