开发者

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

开发者 https://www.devze.com 2023-03-21 13:39 出处:网络
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. marked with @Rep

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. marked with @Repository

several BLogicService services - are Spring beans which have DaoService Autowired and performs some operation on POJOs and persist it in Hibernate. Marked with @Service annotations.

JSF 2.1 Managed Beans - iterate with XHTML pages and hold properties and JSF actions. Marked as @ManagedBean and receive BlLogicServices objects from Spring as @ManagedProperty

and finally XHTML pages which access managed beans.

What would be correct way to manage exception handling in such application? If i have exception on DAO Le开发者_运维百科vel, what would be the correct way to forwatd it to GUI?

If I were working with Spring MVC I would use `@ExceptionHandler, but how can it be done in JSF 2.1 with 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:

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

  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(). You will also need to call FacesContext.getCurrentInstance().renderResponse() to update the view since unhandled exception aborted the JSF lifecycle.

Alternatively you can use try/catch in all your managed beans methods and execute equivalent of myProcessing(t) there. The difference is ExceptionHandler will also catch exceptions during page rendering, not necessarily generated by your classes.


Exceptions should not be passed to the 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; choose a handler for fulfilling the request; package the response, good or bad; 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.

The rest of your question is just confusing the issue. DAOs, MDBs, etc. - all need to be marshalled by a handler of some kind. (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.

0

精彩评论

暂无评论...
验证码 换一张
取 消