SpringBoot web项目错误页定义

举报
yd_221104950 发表于 2020/12/03 00:28:10 2020/12/03
3.1k+ 0 0
【摘要】 SpringBoot web项目错误页定义 1.概述2.使用拦截器实现2.1.定义错误页2.2.定义拦截器2.3.注册拦截器 3.使用Thymeleaf的默认错误页实现 1.概述 错误页有很多种实现,我这里想分享两种我比较喜欢的错误页处理方式。 2.使用拦截器实现 2.1.定义错误页 我们在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>

  
 

文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_40763897/article/details/106301929

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。