zoukankan      html  css  js  c++  java
  • springboot

    1、总览

    2、代码

    1)、pom.xml

    这里注意:springboot 2.2.0以后默认的freemarker文件后缀为:ftlh。本例用的是2.2.1,所以后缀为ftlh

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
    
    </dependencies>
    View Code

    2)、application.properties

    #必须关闭whitelabel,否则无法导航到错误ftlh页面上
    server.error.whitelabel.enabled=false
    server.error.include-stacktrace=always

    3)、java代码,与https://www.cnblogs.com/yaoyuan2/p/11873110.html一致

    4)、5xx.ftlh

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <style>
            table td{
                vertical-align:top;
                border:solid 1px #888;
                padding:10px;
            }
        </style>
    </head>
    <body>
    <h1>My FreeMarker 5xx Error Page</h1>
    <table>
        <tr>
            <td>Date</td>
            <td>${timestamp?datetime}</td>
        </tr>
        <tr>
            <td>Error</td>
            <td>${error}</td>
        </tr>
        <tr>
            <td>Status</td>
            <td>${status}</td>
        </tr>
        <tr>
            <td>Message</td>
            <td>${message}</td>
        </tr>
        <tr>
            <td>Exception</td>
            <td>${exception!""}</td>
        </tr>
        <tr>
            <td>Trace</td>
            <td>
                <pre>${trace}</pre>
            </td>
        </tr>
    </table>
    </body>
    </html>

    注意:在freemarker中${exception}是空的,导致出错,进而执行不到前模板页面了。因此,此处加上了!"",表示为空或找不到时为""

    5)、404.ftlh

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <style>
            table td{
                vertical-align:top;
                border:solid 1px #888;
                padding:10px;
            }
        </style>
    </head>
    <body>
    <h1>My FreeMarker 404 Error Page</h1>
    <table>
        <tr>
            <td>Date</td>
            <td>${timestamp?datetime}</td>
        </tr>
        <tr>
            <td>Error</td>
            <td>${error}</td>
        </tr>
        <tr>
            <td>Status</td>
            <td>${status}</td>
        </tr>
        <tr>
            <td>Message</td>
            <td>${message}</td>
        </tr>
        <tr>
            <td>Exception</td>
            <td>${exception!"No exception thrown"}</td>
        </tr>
        <tr>
            <td>Trace</td>
            <td>
                <pre>${trace!"No Stacktrace available"}</pre>
            </td>
        </tr>
    </table>
    </body>
    </html>
    View Code

    6)、error.ftlh

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <style>
            table td{
                vertical-align:top;
                border:solid 1px #888;
                padding:10px;
            }
        </style>
    </head>
    <body>
    <h1>My FreeMarker Custom Global Error Page</h1>
    <table>
        <tr>
            <td>Date</td>
            <td>${timestamp?datetime}</td>
        </tr>
        <tr>
            <td>Error</td>
            <td>${error}</td>
        </tr>
        <tr>
            <td>Status</td>
            <td>${status}</td>
        </tr>
        <tr>
            <td>Message</td>
            <td>${message}</td>
        </tr>
        <tr>
            <td>Exception</td>
            <td>${exception!"No exception"}</td>
        </tr>
        <tr>
            <td>Trace</td>
            <td>
                <pre>${trace!"No trace"}</pre>
            </td>
        </tr>
    </table>
    </body>
    </html>
    View Code

    3、执行

     

     

     

    总结:

    1、目录结构为:

    templates

    |_error.ftlh

    |_error

      |_5xx.ftlh

      |_404.ftlh

    2、application.properties中,必须加入:

    server.error.whitelabel.enabled=false

    如果想显示trace,也要加入:

    server.error.include-stacktrace=always
  • 相关阅读:
    ResGen.exe 生成resources文件方法 [转]
    C#【Winform】带参启动外部EXE
    SBO的5个开发原则机遇只给有准备的人[转]
    在SQL中插入临时表时使用自动增长的数据字段
    c# 强制退出
    C#实现SQL全库检索数据比较使用DataReader与DataAdapter+Datatable效率,差距惊人!
    推荐一个C#代码混淆器 .NET Reactor
    面向对象软件设计——设计模式学习
    AbstarctFactory模式——设计模式学习
    插入排序算法(直接,折半,希尔)
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/11874513.html
Copyright © 2011-2022 走看看