You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 21:43:15