在Spring Boot中使用Thymeleaf模板引擎迭代列表时遇到"Exception evaluating expression"错误,通常是由于Thymeleaf表达式语法错误或数据绑定问题导致的。以下是一些基础概念、可能的原因以及解决方法:
${}
来执行变量表达式,*{}
来进行选择变量表达式,以及@{}
来进行URL表达式。假设我们有一个Item
类和一个控制器方法来处理请求:
public class Item {
private String name;
// getters and setters
}
@Controller
public class ItemController {
@GetMapping("/items")
public String listItems(Model model) {
List<Item> items = Arrays.asList(new Item("Item1"), new Item("Item2"));
model.addAttribute("items", items);
return "item-list";
}
}
在item-list.html
模板中:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Item List</title>
</head>
<body>
<ul>
<li th:each="item : ${items}" th:text="${item.name}"></li>
</ul>
</body>
</html>
通过以上步骤,你应该能够解决在Thymeleaf Spring Boot中迭代列表时遇到的"Exception evaluating expression"错误。如果问题仍然存在,建议检查具体的异常信息和堆栈跟踪,以便进一步定位问题所在。
领取专属 10元无门槛券
手把手带您无忧上云