我遇到问题模板解析期间发生错误(模板:“类路径资源[templates/update-customer.html]”) org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源[templates/update-customer.html]”),无法重定向到 我的
ùpdate-customer.html
因为这个问题。
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 theCostumerService){
customerService = theCostumerService;
}
@GetMapping("/form-update-customer/{id}")
public String formUpdateCustomer(@PathVariable("id") int id, Model model) {
CostumerModel costumerModel = 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-costumer/' + *{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>
我猜
Update
按钮是对的,请返回给我localhost:8080/customers/form-update-customer/8
。
update-customer.html
中的表单标签应该是:
<form th:action="@{'/customers/update-costumer/' + ${customer.id}}" th:object="${customer}"
method="POST">
或
<form th:action="@{/customers/update-costumer/{id}(id = ${customer.id})}" th:object="${customer}"
method="POST">
两者都有效。关键点是
th:object="${customer}
,将控制器中添加的模型变量“customer”绑定到表单数据对象模型 (DOM)。完成后,您可以像这样访问对象属性:
${customer.id}
或
*{name}