没有 web.xml 错误页面 javaconfig 的 Servlet 3.0 [英] Servlet 3.0 without web.xml error-page javaconfig

查看:29
本文介绍了没有 web.xml 错误页面 javaconfig 的 Servlet 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

web.xml文件中,错误页面如下.

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

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

没有 web.xml javaconfig 转换

without web.xml javaconfig convert

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

覆盖搜索无错误页面

javaconfig 如何转换?

How to javaconfig convert?

推荐答案

使用以下内容创建一个类:

Create a class with the following:

@ControllerAdvice
public class ExceptionHandling {

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

@ControllerAdvice 必须经过组件扫描,因此根据您的配置,它必须位于 sg.ani.api.controller 中的某个位置.这将捕获所有异常并将它们定向到错误页面.所以只需创建一个新的错误页面,你应该没问题.

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.

这篇关于没有 web.xml 错误页面 javaconfig 的 Servlet 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆