Servlet 3.0 without web.xml error-page javaconfig

試著忘記壹切 提交于 2019-12-12 02:09:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/39437141/servlet-3-0-without-web-xml-error-page-javaconfig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!