0

I have the following controller code. This will show a list of banks in the front end in a table with in a jquery tabbed pane. /bankList.do is called when the user clicks on of the tab. banksList.jsp will be rendered with in the tab on success.

@RequestMapping(value = "/banksList", method = RequestMethod.GET)
public ModelAndView banksList() throws Exception {
   BankList banksList = bankService.list();
   return new ModelAndView("banksList", "banksList", banksList.getBanks());
}

I dont know how to handle the error/exceptions that are thrown in the bankend. When there is exception in the bank end, i want to show the user with the following text "Error while communicating to backend, please try again later." without displaying the table in the tabbed pane. How do i make changes in the above controller in order to implement the error functionality.

nagendra
  • 593
  • 1
  • 9
  • 29
  • Possible duplicate? http://stackoverflow.com/questions/2538031/spring-mvc-best-practice-handling-unrecoverable-exceptions-in-controller – seanhodges Feb 07 '12 at 11:22

2 Answers2

2

You can define @Exceptionhandler-annotated methods to handle your service-layer exceptions. See 16.11 Handling exceptions and especially 16.11.2 @ExceptionHandler

skaffman
  • 398,947
  • 96
  • 818
  • 769
1

This is one way, add something like this to your controller class ...

@ExceptionHandler({SomeException.class, SomeOtherException.class})
public String doException(final Exception e, final HttpSession session) {
    LOGGER.error("something failed", e);
    session.setAttribute(UPLOAD_STATUS, false);
    session.setAttribute(ERROR_MESSAGE, e.getMessage());
    return "redirect:" + VIEW_NAME;
}
Paul McKenzie
  • 19,646
  • 25
  • 76
  • 120