Thymeleaf 模板解析错误

Thymeleaf template parsing error

我尝试加载 localhost:8080/.

时遇到解析错误

我在模板中找不到任何错误,为什么会出现这个错误?

错误

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Apr 20 16:59:56 EEST 2015
There was an unexpected error (type=Internal Server Error, status=500).
Exception parsing document: template="index", line 26 - column 3

模板(HTML)

<tr th:each="customer : ${customers}">
    <td th:text="${customer.identity}">001</td>
    <td th:text="${customer.name}">Name</td>
    <td th:text="${customer.address}">Address</td>
    <td th:text="${customer.age}">Age</td>
</tr>

查看(Class)

public String mainPage(Model model){
    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
    PersonJDBCTemplate personJDBCTemplate = (PersonJDBCTemplate) context.getBean("personJDBCTemplate");
    List<Person> persons = personJDBCTemplate.getAllPersons();
    model.addAttribute("customers", persons);
    return "index";
}

可能您在某处缺少结束标记。我不知道你在 HTML 模板中有什么,除非你 post 完整的代码。

但是用这个模板替换你当前的文件。它应该有效。然后您可以将缺少的代码添加到其中。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en"></head>
<body>
<tr th:each="customer : ${customers}">
    <td th:text="${customer.identity}">001</td>
    <td th:text="${customer.name}">Name</td>
    <td th:text="${customer.address}">Address</td>
    <td th:text="${customer.age}">Age</td>
</tr>
</body>
</html>

您的模板名称可能拼错了!

这让我很生气。所有结束标签都通过在线工具验证,但当您在控制器中定义的模板引擎名称与实际文件名不完全匹配时,这对您没有帮助。

注意 此答案解决了一个可能但不太可能的错误解释。它更适合来自搜索的人;导致 OP 到 post 的具体原因很可能是由于缺少结束标记造成的。然而,谁知道呢?不包括控制器代码。