Thymeleaf模板解析报错求助:Spring Boot白标错误页问题排查
Spring Boot Thymeleaf模板解析错误修复方案
错误根源定位
从报错信息可直接锁定核心问题:
home.html第2行的Thymeleaf表达式存在语法闭合错误- 模板片段引用存在大小写不匹配和目标元素缺失问题
1. 修复home.html的语法与引用错误
原代码存在表达式不闭合、引用元素不存在、fragment名称大小写不一致问题,修改后代码如下:
<!doctype html> <html lang="en" xmlns:th="http://www.thymeleaf.org" th:replace="base::layout(~{::body})"> <head> <meta charset="UTF-8" /> <title>this is home page</title> </head> <body> <h1>this is home page</h1> </body> </html>
修改说明:
- 补全表达式闭合符号:将
~{::section)修正为~{::body}),解决语法解析失败问题 - 统一fragment名称大小写:将
Layout改为layout(与base.html中th:fragment="layout(content)"保持一致,Thymeleaf对fragment名称大小写敏感) - 替换目标元素:原代码引用的
section标签不存在,改为引用body标签传递页面内容
2. 修正base.html的内容引用逻辑
原代码中内容插槽写法不规范,修改内容区域代码:
<!-- 原代码 --> <div th:replace="${content}"></div> <!-- 修改后 --> <div th:insert="${content}"></div>
说明:
th:insert会保留当前容器标签,更适合作为内容插槽;若需完全替换容器,可继续使用th:replace,两种写法均能正常工作。
3. 验证依赖与Controller
你的Controller代码完全正确,只需确保项目已引入Thymeleaf starter依赖:
Maven依赖(pom.xml)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Gradle依赖(build.gradle)
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
测试验证
重启Spring Boot应用后访问/home,页面将正常渲染,不再出现白标错误。
内容的提问来源于stack exchange,提问作者qwerty
相关产品推荐
相关产品推荐

