我是 Spring Boot 新手,请帮助我解决此问题。
这是我的 Controller 映射,我需要打印 View 中列表对象的所有属性。
@RequestMapping(value = "/get" , method = RequestMethod.GET)
@ModelAttribute("todolist")
public List<Todo> getuser() {
return (List<Todo>) todoRepository.findAll();
}
这是我的 View ,以及 link to my GitHub project 。提前致谢。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<head>
</head>
<body>
<table>
<tr th:each="message : ${todolist}">
<td th:text="${todolist.title}">Title</td>
<td th:text="${todolist.description}">Description</td>
</tr>
</table>
</body>
</html>
最佳答案
您需要使用message
而不是todolist
<tr th:each="message : ${todolist}">
<td th:text="${message.title}">Title</td>
<td th:text="${message.description}">Description</td>
</tr>
因为 th:each
将迭代 todolist
列表并将值放入 message
属性中。意味着 message
是列表中一个元素的变量名称。例如,最好将其称为 todo
。
关于java - 从字符串启动 Controller 打印列表对象以查看(HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42579653/