thymeleaf设置自定义错误处理页面404,500等

thymeleaf设置自定义错误处理页面404,500等

springboot默认错误处理机制

1)浏览器,返回一个默认的错误界面

2)其他客户端默认相应一个json数据

观察源码:

@Controller
@RequestMapping("${server.error.path:${error.path:/error}}")
public class BasicErrorController extends AbstractErrorController {
@RequestMapping(produces = "text/html")//产生html类型的数据,浏览器发送的请求来到这个方法处理
    public ModelAndView errorHtml(HttpServletRequest request,
            HttpServletResponse response) {
        HttpStatus status = getStatus(request);
        Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
                request, isIncludeStackTrace(request, MediaType.TEXT_HTML)));
        response.setStatus(status.value());
        ModelAndView modelAndView = resolveErrorView(request, response, status, model);
        return (modelAndView == null ? new ModelAndView("error", model) : modelAndView);
    }
​
    @RequestMapping
    @ResponseBody//产生json数据,其他客户端来到这个方法处理
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request,
                isIncludeStackTrace(request, MediaType.ALL));
        HttpStatus status = getStatus(request);
        return new ResponseEntity<Map<String, Object>>(body, status);
    }
    private static class ErrorPageCustomizer implements ErrorPageRegistrar, Ordered {
​
        private final ServerProperties properties;
​
        protected ErrorPageCustomizer(ServerProperties properties) {
            this.properties = properties;
        }
​
        @Override
        public void registerErrorPages(ErrorPageRegistry errorPageRegistry) {
            ErrorPage errorPage = new ErrorPage(this.properties.getServletPrefix()
                    + this.properties.getError().getPath());
            errorPageRegistry.addErrorPages(errorPage);
        }
​
        @Override
        public int getOrder() {
            return 0;
        }
​
    }
    @Configuration
    static class DefaultErrorViewResolverConfiguration {
​
        private final ApplicationContext applicationContext;
​
        private final ResourceProperties resourceProperties;
​
        DefaultErrorViewResolverConfiguration(ApplicationContext applicationContext,
                ResourceProperties resourceProperties) {
            this.applicationContext = applicationContext;
            this.resourceProperties = resourceProperties;
        }
​
        @Bean
        @ConditionalOnBean(DispatcherServlet.class)
        @ConditionalOnMissingBean
        public DefaultErrorViewResolver conventionErrorViewResolver() {
            return new DefaultErrorViewResolver(this.applicationContext,
                    this.resourceProperties);
        }
​
    }

因为代码和老师讲的不太一样,但是这几个类随便点点就大概知道了

一旦出现4xx或者5xx之类的错误,ErrorPageCustomizer就会生效(定制错误界面的显示规则);就会发送来到/error请求;就会被BasicErrorContrller处理

响应去哪个界面是由DefaultErrorViewResolver决定的

​
        @Bean
        @ConditionalOnBean(DispatcherServlet.class)
        @ConditionalOnMissingBean
        public DefaultErrorViewResolver conventionErrorViewResolver() {
            return new DefaultErrorViewResolver(this.applicationContext,
                    this.resourceProperties);
        }

 

如何自定义Error界面)

1)在templates目录下面添加error目录

2)添加404.html,500.html,4xx.html优先匹配最合适的

 

 

默认拥有的数据:

timestamp

status

error

message

path

在前台显示:

    <h1>status:[[${status}]]</h1>
    <h2>timestamp:[[${timestamp}]]</h2>

添加自定义信息:

创建自定义异常处理器

@ControllerAdvice//成为异常处理器
public class MyExceptionHandler {
​
    //1、浏览器客户端返回的都是json:没有自适应
//    @ResponseBody
//    @ExceptionHandler(UserNotExistException.class)//Exception.class处理全部异常
//    public Map<String,Object> handleException(Exception e){
//        Map<String,Object> map = new HashMap<>();
//        map.put("code","user.notexist");
//        map.put("message",e.getMessage());
//        return map;
//    }
​
    //2.有自适应
    //我们处理返回得到的json,其他客户端得到的是网页
    @ExceptionHandler(UserNotExistException.class)//要处理的异常
    public String handleException(Exception e, HttpServletRequest request){//加上HttpServletRequest传入状态码
        Map<String,Object> map = new HashMap<>();
        //传入我们自己的错误状态码  4xx 5xx
        /**
         * Integer statusCode = (Integer) request
         .getAttribute("javax.servlet.error.status_code");
         */
        request.setAttribute("javax.servlet.error.status_code",500);//设置状态码
        map.put("code","user.notexist");
        map.put("message","用户出错啦");
​
        request.setAttribute("ext",map);//将自己定制数据携带到错误界面
        //转发到/error
        return "forward:/error";
    }
}
​

自定义错误界面不生效:

查看error文件夹是否在templates目录下

发现一个写的很细的关于这个的https://www.jianshu.com/p/be27d6288a67

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
如果你在使用 Thymeleaf 时遇到了页面报错,可以查看控制台输出的错误信息,找到具体的问题所在。常见的错误包括语法错误、模板引用错误、标签使用错误等等。 如果你想利用 Thymeleaf 自定义错误页面,可以按照以下步骤进行: 1. 首先在 `src/main/resources/templates` 目录下创建一个 `error` 目录。在该目录下创建一个名为 `error.html` 的 Thymeleaf 模板文件。 2. 在 `application.properties` 文件中配置错误处理的地址: ``` server.error.path=/error ``` 3. 创建一个名为 `ErrorController` 的控制器类,用于处理错误请求: ```java @Controller public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController { @RequestMapping("/error") public String handleError(HttpServletRequest request, HttpServletResponse response) { // 获取错误状态码 Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); // 根据状态码返回不同的错误页面 if (statusCode == 404) { return "error/404"; } else { return "error/500"; } } @Override public String getErrorPath() { return "/error"; } } ``` 4. 在 `error` 目录下创建 `404.html` 和 `500.html` 两个 Thymeleaf 模板文件,用于显示不同类型的错误页面。 例如,`404.html` 可以这样编写: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>404 Not Found</title> </head> <body> <h1>404 Not Found</h1> <p>您请求的页面不存在,请检查您输入的地址是否正确。</p> </body> </html> ``` 5. 访问不存在的页面,尝试触发 404 错误,可以看到自定义的错误页面。 如果你想测试 500 错误,可以在控制器中抛出一个异常,例如: ```java @RequestMapping("/test") public String test() { throw new RuntimeException("测试错误"); } ``` 然后访问 `/test` 地址即可。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值