广告
返回顶部
首页 > 资讯 > 后端开发 > Python >springboot自定义404、500错误提示页面的实现
  • 532
分享到

springboot自定义404、500错误提示页面的实现

2024-04-02 19:04:59 532人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录SpringBoot 默认的异常处理机制使用模板引擎使用示例没有使用模板引擎springboot 默认的异常处理机制 springboot 默认已经提供了一套处理异常的机制。一旦

springboot 默认的异常处理机制

springboot 默认已经提供了一套处理异常的机制。一旦程序中出现了异常 springboot 会向 /errorurl 发送请求。在 springboot 中提供了一个名为 BasicErrorController 的类来处理 /error 请求,然后跳转到默认显示异常的页面来展示异常信息

使用模板引擎

在使用 thymeleaf 等模板引擎时,springboot 会自动到 src/main/resources/templates/error/,文件夹下寻找 404.html、500.html 的错误提示页面

错误提示页面的命名规则就是:错误码.html,如 404404.html500500.html

使用示例

创建 springboot 项目如下

404、500 错误提示页面结构如下

在这里插入图片描述

application.properties 项目配置文件


server.port=8080

#它的默认值就是classpath:/templates/,源码在ThymeleafProperties类中
spring.mvc.view.prefix=classpath:/templates/
#它的默认值就是.html,源码在ThymeleafProperties类中
spring.mvc.view.suffix=.html
spring.thymeleaf.cache=false

404 页面内容如下


<!DOCTYPE html>
<html lang="en" xmlns:th="Http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>404</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow"  rel="external nofollow" />
    <link rel="stylesheet" type="text/CSS" th:href="@{/css/404.css}" rel="external nofollow" />
</head>
<body>
	<div id="banner" style="height: 600px;width: 600px;margin-left: 370px"></div>
</body>
</html>

500 页面内容如下


<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>500</title>
    <link rel="shortcut icon" type="image/x-icon" th:href="@{/img/favicon.ico}" rel="external nofollow"  rel="external nofollow" />
    <link rel="stylesheet" type="text/css" th:href="@{/css/500.css}" rel="external nofollow" />
</head>
<body>
    <div id="banner" style="height: 600px;width: 600px;margin-left: 370px"></div>
</body>
</html>

controller 如下


@Controller
public class PageController {

    // 跳转到登录页
    @GetMapping(path = "/toLogin")
    public String toLogin() {
        int code = 1/0;
        return "login";
    }
}

404.html 页面测试

访问不存在的接口:http://localhost:8080/aaaa,结果如下

在这里插入图片描述

500.html 页面测试

访问已存在的接口:http://localhost:8080/toLogin,结果如下

在这里插入图片描述

没有使用模板引擎

如果没有使用 thymeleaf 等模板引擎时,springboot 会到静态资源文件夹寻找 404.htm、500.html的错误提示页面,命名同上。springboot 中默认的静态资源路径有 4 个,分别是

  • classpath:/METAINF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

优先级顺序为:META-INF/resources > resources > static > public,以上 4 种路径创建 error 文件夹,再创建 404、500 错误提示页面如下

在这里插入图片描述

不用写额外的映射器,就能直接请求到

到此这篇关于springboot 自定义404、500错误提示页面的实现的文章就介绍到这了,更多相关springboot 自定义错误页面内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: springboot自定义404、500错误提示页面的实现

本文链接: https://www.lsjlt.com/news/158634.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • springboot自定义404、500错误提示页面的实现
    目录springboot 默认的异常处理机制使用模板引擎使用示例没有使用模板引擎springboot 默认的异常处理机制 springboot 默认已经提供了一套处理异常的机制。一旦...
    99+
    2022-11-12
  • springboot怎么自定义404、500错误提示页面
    本篇内容介绍了“springboot怎么自定义404、500错误提示页面”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!springboot ...
    99+
    2023-06-21
  • java自定义错误页面实现方法
    java后台自定义错误页面:(推荐:java视频教程)java后台项目中,经常会出现404或500等错误,如果不做设置,服务器会默认返回404或500的错误页面给前端显示错误页面。掌握了错误页面设置,就可以根据业务需要自定义404或500错...
    99+
    2019-01-29
    java
  • SpringBoot实现自定义配置文件提示的方法
    目录SpringBoot如何实现自定义配置文件提示1、编写一个配置类 2、引入自动提示插件 2.1、引入插件2.2、重新编译3、配置验证SpringBoot如何实...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作