在SpringBoot中配置Thymeleaf的模板路径方式

Nissa ·
更新时间:2024-09-21
· 915 次阅读

目录

配置Thymeleaf的模板路径

关于thymeleaf配置说明

配置Thymeleaf的模板路径

众所周知,Thymeleaf的模板文件默认是在项目文件夹的src\main\resources\templates目录下的。

不过出于特殊需要,要修改其路径怎么办呢?

在我们的项目配置文件application.properties中,添加如下配置:

#Thymeleaf配置 spring.thymeleaf.prefix=自定义的Thymeleaf的模板位置,jar内部以classpath:开头,外部路径使用file:开头 spring.thymeleaf.suffix=自动匹配后缀 spring.thymeleaf.cache=是否使用缓存

知道了以上配置的作用,我们就知道了,默认情况下Thymeleaf的spring.thymeleaf.prefix这条配置的值是classpath:/templates/(Spring中classpath的根目录即对应项目文件夹的src\main\resources)

这里放出我的配置:

#Thymeleaf配置 spring.thymeleaf.prefix=file:Resources/thymeleaf/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false

即把模板路径设置为当前目录下(指项目根目录或者编译后运行jar时的运行目录)的Resources/thymeleaf下。

需要注意的是模板路径值最后一定要加上斜杠/,否则就会出错。

关于thymeleaf配置说明

thymeleaf是一种模板引擎,可以查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。

引入依赖,在pom.xml文件添加以下内容。

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

在application.yml的配置文件里加入以下内容

thymeleaf:     cache: false # 关闭页面缓存     encoding: UTF-8 # 模板编码     prefix: classpath:/templates/  # 页面映射路径     suffix: .html # 构建URL时附加到查看名称的后缀     mode: HTML5 # 模板模式

cache属性默认值是true,把他设置为false,便于我们进行调试,不必每次修改都重启一遍项目。

encoding属性是模板编码

prefix属性是页面映射路径

suffix属性是构建URL时附加到查看名称的后缀

mode属性是模板模式

注意:.yml与.properties文件均是spring boot的配置文件,其中.yml注重缩进和空格,通过缩进来表示父子级关系

以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。 



thymeleaf springboot 模板

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章