众所周知,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下。

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

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐