• Spring Boot 自定义错误页面


    定义静态的错误页面

    在 resources 下的 static 目录下,新建 error 目录,在其中新建各种静态错误页面,如 404、500,也可以模糊处理,如4xx、5xx 等。

    如下图:

    当程序运行出错时,会自动根据错误代码(如500)找到相应的错误页面(如/static/error/500.html),给予展示。

    定义动态的错误页面

    在 templates 目录定义动态的错误页面。

    定义动态的错误页面,需要使用页面模板,如 thymeleaf、freemarker 等

    使用页面模板,只需在 pom.xml 添加页面模板的依赖即可,如:

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    在 resources 下的 templates 目录下,新建 error 目录,在其中新建各种静态错误页面,如 404、500,也可以模糊处理,如4xx、5xx 等。

    如下图:

    错误页面的展示优先级

    1、精确大于模糊

    2、动态大于静态

    如,程序抛出 500 错误,但在 templates 目录下有 500.html 和 5xx.html 两个文件,则首先展示 500.html

    又如,程序抛出 500 错误,但在 templates 目录和 static 目录下都有 500.html 文件,则优先展示templates 目录下的 500.html 文件。

    参考资源:http://www.javaboy.org/2019/0417/springboot-exception.html

    每天学习一点点,每天进步一点点。

  • 相关阅读:
    SCUT
    模板
    重链剖分
    树的重心
    SCUT
    SCUT
    SCUT
    SCUT
    SCUT
    SCUT
  • 原文地址:https://www.cnblogs.com/youcoding/p/13186489.html
Copyright © 2020-2023  润新知