Q:

With Spring MVC can you make one controller call another controller to handle an error page?

I have a controller which can throw an exception. When it does throw an exception it will show my errorpage.jsp, instead of the jsp page I wanted to show. An exxample of this controller is:
public class myController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> model = new HashMap<>();
        // Some logic which can throw an error....

        return model;
    }
}

The application-servlet.xml looks like this:
...
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/login.html">myController</prop>
            <prop key="/errorpage.html">myErrorController</prop>
        </props>
    </property>
</bean>
...

Normally when an error, like navigating to a page that doesn't exist, occurs the error controller will be called which will return a hashmap of variables for the error page jsp. However, when the login controller encounters an error it will show the error page, but it won't call the error controller which would provide some of these parameters, so the page is incomplete.
Is there some way to define in the servlet.xml for the myController to call the myErrorController when it encounters an error and redirects to the error page?

A:

You can annotate a controller class with @ControllerAdvice which will be shared between all Controllers. Then using the @ExceptionHandler annotation you can create methods to handle specific exceptions.
Example:
@ControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(MyException.class)
    public ModelAndView handleException(MyException e, HttpServletRequest request, HttpServletResponse response) { // included servlet request and response
        ModelAndView model = new ModelAndView("error/errorpage.jsp");
        model.addObject("exception", e);
        return model;
    }

}

And anytime you throw MyException in any of your other controllers, this controller advice method will handle the exception for you! As the exception, servlet request and response is passed to the ExceptionHandler method, all of that information is available to you.
Example in another controller:
@Controller
public class SomeController {

   @Autowired
   private DataService dataService;

   @RequestMapping(value = '/', method = RequestMethod.POST)
   public String doSomething() {
      return dataService.foobar(); // throws MyException which is handled with ExceptionHandler
   }

}

