所以我基本上是使用 spring boot、thymleaf、html、css 制作一个电子商务项目。我在解析 thymeleaf 模板时遇到服务器错误,该模板是
There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/EditProduct.html]")
@GetMapping("/edit")
public String showEditPage(
Model model,
@RequestParam int id
) {
try {
Product product = repo.findById(id).get();
model.addAttribute("product", product);
ProductDto productDto = new ProductDto();
productDto.setName(product.getName());
productDto.setBrand(product.getBrand());
productDto.setCategory(product.getCategory());
productDto.setPrice(product.getPrice());
productDto.setDescription(product.getDescription());
model.addAttribute("productDto", productDto);
}
catch(Exception ex){
System.out.println("Execption: " + ex.getMessage());
return "redirect:/products";
}
return "EditProduct";
}
}
<form method="post" enctype="multipart/form-data" th:object="${productDto}">
<div class="row mb-3">
<label class="col-sm-4 col-form-label">ID</label>
<div class="col-sm-8">
<input readonly class="form-control-plaintext" th:field="${product.id}">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label">Name</label>
<div class="col-sm-8">
<input class="form-control" th:field="${productDto.name}">
<p th:if="${#fields.hasErrors('name')}" th:errorclass="text-danger th:errors="${productDto.name}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label">Brand</label>
<div class="col-sm-8">
<input class="form-control" th:field="${productDto.brand}">
<p th:if="${#fields.hasErrors('brand')}" th:errorclass="text-danger" th:errors="${productDto.brand}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label">Category</label>
<div class="col-sm-8">
<select class="form-select" th:field="${productDto.category}">
<option value='Motherboard'>Motherboard</option>
<option value='Ram'>Ram</option>
<option value='Graphics Card'>Graphics Card</option>
<option value='Storage'>Storage</option>
<option value='PowerSupplyUnit'>PowerSupplyUnit</option>
<option value='Cabinet'>Cabinet</option>
<option value='Monitor'>Monitor</option>
<option value='Keyboard'>Keyboard</option>
<option value='Mouse'>Mouse</option>
<option value='Gaming Headset'>Gaming Headset</option>
</select>
<p th:if="${#fields.hasErrors('category')}" th:errorclass="text-danger" th:errors="${productDto.category}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label">Price</label>
<div class="col-sm-8">
<input class="form-control" type="number" step="0.01" min="0" th:field="${productDto.price}">
<p th:if="${#fields.hasErrors('price')}" th:errorclass="text-danger" th:errors="${productDto.price}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label">Description</label>
<div class="col-sm-8">
<textarea class="form-control" th:field="${productDto.description}"></textarea>
<p th:if="${#fields.hasErrors('description')}" th:errorclass="text-danger"
th:errors="${productDto.description}"></p>
</div>
</div>
<div class="row mb-3">
<div class="offset-sm-4 col-sm-8">
<img th:src="@{'/images/' + ${product.imageFilename}}"
alt="..." width="150">
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label">Image</label>
<div class="col-sm-8">
<input class="form-control" type="file" th:field="${productDto.imageFile}">
<p th:if="${#fields.hasErrors('imageFile')}" th:errorclass="text-danger"
th:errors="${productDto.imageFile}"></p>
</div>
</div>
<div class="row mb-3">
<label class="col-sm-4 col-form-label">Created At</label>
<div class="col-sm-8">
<input readonly class="form-control-plaintext" th:value="${product.createdAt}">
</div>
</div>
<div class="row">
<div class="offset-sm-4 col-sm-4 d-grid">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<div class="col-sm-4 d-grid">
<a class="btn btn-outline-primary" href="/products" role="button">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
所以这是我的 html thymleaf 模板,我收到以下错误: 此应用程序没有 /error 的显式映射,因此您将其视为后备。
2024 年 7 月 18 日星期四 22:35:50(太平洋夏令时) 出现意外错误(类型=内部服务器错误,状态=500)。 模板解析时发生错误(模板:“类路径资源[templates/EditProduct.html]”) org.thymeleaf.exceptions.TemplateInputException:模板解析时发生错误(模板:“类路径资源[templates/EditProduct.html]”)
通常
Thymeleaf
会详细说明失败的原因,请遵循堆栈跟踪。
检查 ProductDto
是否具有 imageFile
字段,并且 Product
是否具有 createdAt
、imageFilename
。并且,如上所述,修复第 12 行。
我尝试了该片段,它显示了这些练习后的页面。但问题可能出在
<form>
区块之外