zl程序教程

您现在的位置是:首页 >  后端

当前栏目

收藏夹吃灰系列(十二):Springboot配置Thymeleaf实现静态页面访问 | 超级详细,建议收藏!

SpringBoot配置静态 实现 系列 详细 建议 访问
2023-09-27 14:28:25 时间

一、前言

Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎。所以这里介绍一下springboot结合Thymeleaf,实现模板实例以及途中遇到的问题。

二、配置与使用

1.引入jar。 在pom中加入thymeleaf对应的starter 依赖。

        <!--模板引擎Thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.在配置文件(application-dev.yml)中配置Thymeleaf模板参数。

#spring配置
spring:
  thymeleaf:
    cache: false
    mode: LEGACYHTML5
    prefix: classpath:/templates/
    suffix: .html

重要参数解说:

cache: 是否缓存,开发模式下设置为false,避免改了模板还要重启服务器,线上设置为true,可以提高性能。一般改为false。

mode:配置视图模板类型,如果使用htm