昨天用SpringBoot整合Thymeleaf搞了好长时间,一直有问题,记录一下解决方案,希望帮助大家少走弯路。
1 模版依赖未导入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2 一些配置信息
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
encoding: UTF-8
mode: HTML5
cache: false
enabled: true
我的问题不在这里,但是很多解决方案都说修改这个可以成功
也有一些说设置成下面这样。
spring.thymeleaf.mode=LEGACYHTML5
3 SpringBoot对html的严格读
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
4 版本问题
这个版本不兼容问题是解答里说的最多的。
需要在pom文件里的properties标签指定版本
<properties>
<java.version>1.8</java.version>
<thymeleaf-spring5.version>3.0.9.RELEASE</thymeleaf-spring5.version>
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
复现了一下,当时以为是版本问题
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [login], template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE
5 打包问题
我的问题就是这个,我因为先整合的mybatis的依赖,
加入了如下
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.yml</include>
</includes>
</resource>
把这段删了或者加上
<resource>
<directory>src/main/resources</directory>
</resource>
即可,
我最终是这个问题。
写在最后
写的时候一直提示获取不到资源,应该往这方面想。
希望对大家有用。