Search code examples
javaxmlspringservletsweb.xml

Servlet 3.0 without web.xml error-page javaconfig


In the web.xml file, error page as follows.

<error-page>
   <location>/WEB-INF/jsp/admin/ErrorPage.jsp</location>
</error-page>

without web.xml javaconfig convert

@Configuration
@EnableWebMvc
@ComponentScan("sg.ani.api.controller")
public class WebConfig extends WebMvcConfigurerAdapter{
}

override search no error page

How to javaconfig convert?


Solution

  • Create a class with the following:

    @ControllerAdvice
    public class ExceptionHandling {
    
      @ExceptionHandler(value=Exception.class)
      public String showErrorPage(){
        return "error";
      }
    }
    

    The @ControllerAdvice must be component-scanned so based on your config it must be somewhere in sg.ani.api.controller. This will catch all exceptions and direct them to the error page. So just create a new error page and you should be fine.