3

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. } } } 

1 Answer 1

5

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; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks vinay. This is perfect.
If you are satisfied, would you mind accept it as the answer please? ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.