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

Spring Boot 3+Thymeleaf无法跳转更新页面,模板解析报错

问题描述

使用Spring Boot 3搭配Thymeleaf开发时,点击更新按钮无法跳转至update-customer.html页面,触发模板解析错误:

An error happened during template parsing (template: "class path resource [templates/update-customer.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/update-customer.html]")

点击按钮能生成正确路径localhost:8080/customers/form-update-customer/8,但问题依旧。相关代码如下:

相关代码

list.html

<td>
     <a th:href="@{'/customers/form-update-customer/' + ${customer.id}}" class="btn btn-primary btn-sm">Update</a>
  </td>

CustomerController.java

@Controller
@RequestMapping("/customers")
public class CustomerController {

  private CustomerService customerService;

  public CustomerController(CustomerService theCustomerService){
        customerService = theCustomerService;
  }

  @GetMapping("/form-update-customer/{id}")
    public String formUpdateCustomer(@PathVariable("id") int id, Model model) {
      CustomerModel customerModel = customerService.read(id);
      model.addAttribute("customer", customerModel);
      return "update-customer";
  }

}

update-customer.html

<!doctype html>
    <html lang="en" xmlns:th="http://thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <link rel="stylesheet" th:href="@{/assets/bootstrap-5.3.3.min.css}">
        <script th:src="@{/assets/bootstrap-5.3.3.min.js}"></script>
        <script th:src="@{/assets/jquery-3.7.1.min.js}"></script>
        <script th:src="@{/assets/mask.js}"></script>
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Update Customer</title>
    </head>
    <body>
    <form th:action="@{'/customers/update-customer/' + *{id}}" method="POST">
        <input type="hidden" th:field="*{id}">
        <input type="text" th:field="*{name}" class="form-control mb-4 w-25" placeholder="Input the name"/>
        <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="text-danger"></div>
        <button type="submit" class="btn btn-info col-2">Update</button>
    </form>
    </body>
    </html>
可能的原因及解决方法

1. 表单缺少绑定对象声明

update-customer.html中使用了*{id}这类选择表达式,但没有通过th:object指定绑定的模型对象,导致Thymeleaf无法解析表达式。修改表单代码,添加th:object="${customer}":

<form th:action="@{'/customers/update-customer/' + *{id}}" th:object="${customer}" method="POST">
    <input type="hidden" th:field="*{id}">
    <input type="text" th:field="*{name}" class="form-control mb-4 w-25" placeholder="Input the name"/>
    <div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="text-danger"></div>
    <button type="submit" class="btn btn-info col-2">Update</button>
</form>

2. 模板文件位置或命名错误

确认update-customer.html确实放在src/main/resources/templates/目录下,且文件名大小写完全匹配(Thymeleaf对文件名大小写敏感,比如Update-Customer.html和update-customer.html会被视为不同文件)。

3. CustomerModel缺少getter/setter方法

Thymeleaf需要通过getter/setter访问模型属性,检查CustomerModel类是否为id和name提供了对应的方法:

public class CustomerModel {
    private int id;
    private String name;

    // 必须包含以下getter和setter
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

4. 依赖版本不兼容

Spring Boot 3对应的Thymeleaf版本应为3.1.x及以上,确保依赖由Spring Boot自动管理,避免手动指定过低版本。Maven依赖示例:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

内容的提问来源于stack exchange,提问作者user23450337

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 09:53:22