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

Thymeleaf模板解析报错:DOCTYPE格式问题导致500错误求助

解决Thymeleaf模板解析报错:DOCTYPE clause cannot have a non-null PUBLIC ID and a null SYSTEM ID

核心修复:修正HTML的DOCTYPE声明

你的index.html里的DOCTYPE写法违反了Thymeleaf的解析规则,同时Thymeleaf 3.x版本已不再需要引入旧版DTD。直接替换为标准HTML5 DOCTYPE即可:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">

原因:原声明仅指定了PUBLIC ID却未提供SYSTEM ID,触发Thymeleaf的参数校验异常;且Thymeleaf 3支持原生HTML5,无需引入针对Spring 4的旧DTD文件。

额外优化项

1. 清理POM重复依赖

spring-boot-starter-thymeleaf已经包含了适配Spring Boot的Thymeleaf版本,无需单独引入thymeleaf依赖,删除以下代码:

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.15.RELEASE</version>
</dependency>

2. 修正HTML语法错误

  • 移除th: each中的空格,改为th:each
  • 数据行单元格使用<td>而非<th>(<th>仅用于表头)
  • 实体属性名保持小驼峰匹配(Java类中若为firstName,模板中对应${employee.firstName})

修正后的表格代码:

<tbody>
    <tr th:each="employee : ${listEmployees}">
        <td th:text="${employee.firstName}"></td>
        <td th:text="${employee.lastName}"></td>
        <td th:text="${employee.email}"></td>
    </tr>
</tbody>

验证

修改完成后重启应用,访问根路径即可正常渲染模板。

内容的提问来源于stack exchange,提问作者Aroyewun Sodiq Olawale

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 00:15:37