其他分享
首页 > 其他分享> > springboot下freemarker异常页面报错信息处理全局

springboot下freemarker异常页面报错信息处理全局

作者:互联网

目录

用freemarker做seo引出来一系列的问题,尤其是类似下面页面报错的问题,搞得人很烦。那有没有一种方式只记录错误日志而不用显示在页面上呢,经过多次努力试探(百度),终于找到了一种方式,在此做下记录,方便有缘人。

image-20211201131439892

配置文件

重点是: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前端能写出来一堆问题还要你跟着看,头疼死了,靠!!!

标签:exception,art,springboot,freemarker,handler,报错,template,页面
来源: https://www.cnblogs.com/cdong0109/p/15628525.html