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
  • 相关阅读:
    SPOJ375(树链剖分)
    最短路相关模板、总结
    Linux入门基础#2:Linux文件系统基本结构
    poj 2229 Sumsets (DP)
    Power BI for Office 365(七) Power BI站点
    HDU1045 Fire Net
    Android 系统搜索框(有浏览记录)
    Struts2 Action接收表单参数
    要注意的点
    复习昨天的,继续过Hard题目
  • 原文地址:https://www.cnblogs.com/yaoyuan2/p/11874513.html
Copyright © 2011-2022 走看看