1. 程式人生 > 其它 >springboot下freemarker異常頁面報錯資訊全域性處理

springboot下freemarker異常頁面報錯資訊全域性處理

目錄

用freemarker做seo引出來一系列的問題,尤其是類似下面頁面報錯的問題,搞得人很煩。那有沒有一種方式只記錄錯誤日誌而不用顯示在頁面上呢,經過多次努力試探(百度),終於找到了一種方式,在此做下記錄,方便有緣人。

配置檔案

重點是:template_exception_handler我已經註釋說明 後面的屬性可以是Ignore /你處理freemarker異常的類

spring:
  datasource:
    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/art_park?useUnicode=true&characterEncoding=utf-8&useSSL=false&tinyInt1isBit=false&serverTimezone=Asia/Shanghai
    username: root
    password: 'root'
  redis:
    database: 0
    host: 127.0.0.1
    password:
    port: 6379
    pool:
      max-active: 1000
      max-idle: 100
      max-wait: -1
      min-idle: 1
  freemarker:
    charset: UTF-8 # 模板編碼
    content-type: text/html; charset=utf-8 # Content-Type value.
    suffix: .html # 設定模板的字尾
    template-loader-path: file:C:/soft/ideaProject/art-front/templates # 設定模板的載入路徑,多個以逗號分隔,預設:
    settings:
      template_update_delay: 0
      # classic_compatible: true 這個開啟就沒有錯誤日誌了 作用是過濾頁面上{name}沒有取到值為空的情況
      template_exception_handler: com.leshu.common.config.MyTemplateExceptionHandler
      #template_exception_handler 是用來設定freemark異常處理類的路徑 如果不需要處理,template_exception_handler: Ignore 這種情況下打印不出日誌
    resources:
      static-locations: file:C:/soft/ideaProject/art-front/static/ # 這個是用來從伺服器上任何位置載入css img等靜態資源的

異常類

大概就是這樣的一個簡單東西

@Component
@Slf4j
public class MyTemplateExceptionHandler implements TemplateExceptionHandler {

    @Override
    public void handleTemplateException(TemplateException templateException, Environment environment, Writer out) {
        log.error(templateException.getMessage());
    }
}

建議直接用thymeleaf,直接交給前端寫就好了,freemarker前端能寫出來一堆問題還要你跟著看,頭疼死了,靠!!!

it's my turn to fuck you