thrilling3
thrilling3

Reputation: 33

@ExceptionHandler in Spring

In my application I have configured an error page in web.xml. I need to override that for a specific controller based on a condition. Here when a condition becomes true I need to redirect to a specific error page otherwise normal error page should be rendered. Here is the code. Please help me.

@Controller
public class Test{

 @ExceptionHandler(Exception.class)
 public ModelAndView generateException(HttpServletRequest httpServletRequest){
  if(condition) {
   return new ModelAndView("myError.jsp");
  } else {
   //should execute default error page.
  }
 }
}

Upvotes: 3

Views: 511

Answers (1)

Vinay
Vinay

Reputation: 2817

Throw the exception again which will be handled by DefaultHandlerExceptionResolver to respond with the error page defined in your web.xml; It doesn't invoke the same Exception Handler of the controller.

@ExceptionHandler(Exception.class)
public ModelAndView generateException(Exception ex) throws Exception{
    if(condition) {
        return new ModelAndView("myError.jsp");
    } else {
        //should execute default error page.
        throw ex;
    }   
}

Upvotes: 5

Related Questions