gpt4 book ai didi

java - Thymeleaf:无法解析为链接的表达式

转载 作者:行者123 更新时间:2023-12-01 17:22:53 25 4
gpt4 key购买 nike

我是 thymeleaf 新手,不明白这个错误。

2020-04-16 16:20:24.222 ERROR 18060 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "products": Could not parse as expression: "/@{'/edit/' + ${product.id}}" (template: "products" - line 32, col 24)

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "/@{'/edit/' + ${product.id}}" (template: "products" - line 32, col 24)

这是 html/thymeleaf 代码:

    <tr th:each="product : ${products}">
<td th:text="${product.id}">Product ID</td>
<td th:text="${product.name}">Name</td>
<td th:text="${product.brand}">Brand</td>
<td th:text="${product.madein}">Made in</td>
<td th:text="${product.price}">Price</td>
<td>
<a th:href="/@{'/edit/' + ${product.id}}">Edit</a>
&nbsp;&nbsp;&nbsp;
<a th:href="/@{'/delete/' + ${product.id}}">Delete</a>
</td>
</tr>

以下是适用的 Spring Boot Controller 代码:

@RequestMapping("/edit/{id}")
public ModelAndView showEditProductPage(@PathVariable(name = "id") int id) {
ModelAndView mav = new ModelAndView("edit_product");
Product product = productService.get(id);
mav.addObject("product", product);

return mav;
}

@RequestMapping("/delete/{id}")
public String deleteProduct(@PathVariable(name = "id") int id) {
productService.delete(id);
return "redirect:/";
}

如果我注释掉最后一个 td 部分,则其余部分可以正常工作。有什么建议吗?

最佳答案

尝试使用以下内容:

<tr th:each="product : ${products}">
<td th:text="${product.id}">Product ID</td>
<td th:text="${product.name}">Name</td>
<td th:text="${product.brand}">Brand</td>
<td th:text="${product.madein}">Made in</td>
<td th:text="${product.price}">Price</td>
<td>
<a th:href="@{/edit/{id}(id=${product.id})}">Edit</a>
&nbsp;&nbsp;&nbsp;
<a th:href="@{/delete/{id}(id=${product.id})}">Delete</a>
</td>
</tr>

查看语法here

关于java - Thymeleaf:无法解析为链接的表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61261151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com