SpringBoot web项目错误页定义

1.概述

错误页有很多种实现,我这里想分享两种我比较喜欢的错误页处理方式。

2.使用拦截器实现

2.1.定义错误页

我们在springboot的web项目的资源目录下创建各个错误代码对应的错误页,建好后,如下图所示:

~/Documents/MY_PROJECT/SuperGrocery$ tree -L 8
.
├── src
│   └── main
│       └── resources
│           ├── static
│           │   ├── error
│           │   │   ├── 400.html
│           │   │   ├── 403.html
│           │   │   ├── 404.html
│           │   │   ├── 405.html
│           │   │   ├── 408.html
│           │   │   ├── 500.html
│           │   │   ├── 502.html
│           │   │   ├── 503.html
│           │   │   ├── 504.html
│           │   │   ├── 505.html

2.2.定义拦截器

package com.wong.interceptor;


import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;


/**
 * 错误页面拦截器
 * @Component注解会把配置类初始化好放入spring容器中的
 */
@Component
public class ErrorPageInterceptor extends HandlerInterceptorAdapter {

    private List<Integer> errorCodeList = Arrays.asList(400,403,404, 408,500, 501,502,503,504,505,509);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws
            Exception {
        if (errorCodeList.contains(response.getStatus())) {
            response.sendRedirect("/error/" + response.getStatus());
            return false;
        }
        return super.preHandle(request, response, handler);
    }

}

2.3.注册拦截器

package com.wong.config;

import com.wong.interceptor.ErrorPageInterceptor;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 配置使其可以当请求参数中携带语言参数lang时自动切换语言
 *
 */
@Configuration// 当系统启动时,就会来配置WebConfig
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private ErrorPageInterceptor errorPageInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(errorPageInterceptor);
    }
}

当出错误时,拦截器一拦截到就会转发到错误信息页面。

3.使用Thymeleaf的默认错误页实现

SpringBoot一般都会使用模板引擎,那么这些模板引擎都会提供默认的错误页,不同模板引擎默认的错误页分别是:

  • Thymeleaf模板引擎:error.html
  • FreeMarker模板引擎:error.ftl
  • Velocity模板引擎:error.vm
  • JSP模板引擎:error.jsp

而且SpringBoot为错误视图提供了以下错误属性,可以直接用在错误页模板中,不用写任何控制器:

  • timestamp:错误发生时间
  • status:HTTP状态码
  • error:错误原因
  • exception:异常的类名
  • message:异常消息
  • errors:BindingResult异常里的各种错误
  • trace:异常跟踪信息
  • path:错误发生时请求的URL路径

本文使用Thymeleaf模板引擎,所以在resource/templates下添加error.html页面,springBoot会自动找到该页面作为错误页面。

error.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>[[${status}]]错误</title>
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="/webjars/bootstrap/4.5.0/css/bootstrap.css" data-th-href="@{/webjars/bootstrap/4.5.0/css/bootstrap.css}">
</head>
<body>
<div class="card text-center">
    <div class="card-header">
        <h1 class="card-title">[[${status}]]!</h1>
    </div>
    <div class="card-body">
        <p class="card-text">[[${message}]]</p>
        <a href="/" class="btn btn-primary">返回首页</a>
    </div>

</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值