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 Level, 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:
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):
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.