Spring Boot项目中Thymeleaf的th:text标签不生效求助
问题根源
你同时在项目中引入了Freemarker和Thymeleaf两个模板引擎依赖,并且在application.properties中同时启用了两者。Spring Boot在存在多个模板引擎时,会优先使用Freemarker解析/templates下的.html文件,导致Thymeleaf标签完全未被处理,所以th:text无法生效。
解决方案
按以下步骤调整配置与依赖,确保Thymeleaf正常工作:
1. 移除Freemarker依赖
在pom.xml中删除Freemarker相关依赖块:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
2. 删除Freemarker配置
在application.properties中删除所有以spring.freemarker开头的配置项,仅保留Thymeleaf配置:
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8 spring.thymeleaf.enabled=true spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html
3. 确认模板文件正确性
你的HTML文件已正确引入Thymeleaf命名空间,无需修改:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>111</h1> <div th:text="111"></div> </body> </html>
4. 重启项目
完成上述修改后重启Spring Boot项目,页面将正常渲染出两行"111"。
内容的提问来源于stack exchange,提问作者Can Bin
相关产品推荐
相关产品推荐

