Java 在对象列表中迭代的Thymeleaf显示为空
我试图使用SpringMVC遍历通过ModelMap传递的对象。但是,在浏览器上测试时,文本显示为空白。例如: test.jsp:Java 在对象列表中迭代的Thymeleaf显示为空,java,html,spring-mvc,jsp,thymeleaf,Java,Html,Spring Mvc,Jsp,Thymeleaf,我试图使用SpringMVC遍历通过ModelMap传递的对象。但是,在浏览器上测试时,文本显示为空白。例如: test.jsp: <!DOCTYPE html> <html> <body> Hello ${test} <tr th:each="user,iterStat : ${testList}"> <td th:text="${user.id}" /> <td th:text="${user.name}
<!DOCTYPE html>
<html>
<body>
Hello ${test}
<tr th:each="user,iterStat : ${testList}">
<td th:text="${user.id}" />
<td th:text="${user.name}" />
</tr>
</body>
</html>
这里有什么问题?您的模板名是
.jsp
,但您似乎在使用Thymeleaf(这是我的建议)。也许你想把它命名为.html
?@chrylis啊,是的。我真是太愚蠢了。文件名应为.html。决定从JSP移植到Thymeleaf,但没有重命名文件。
@GetMapping("/test")
public String test(Model model) {
model.addAttribute("test", "Bob");
List<ChatUser> users = new ArrayList<>();
users.add(new ChatUser("asdf"));
users.add(new ChatUser("ewfasdf"));
model.addAttribute("testList", users);
return "test";
}
<!DOCTYPE html>
<html>
<body>
Hello Bob
<tr th:each="user,iterStat : [com.mkyong.ChatUser@7a5dd809, com.mkyong.ChatUser@61f4628]">
<td th:text=""/>
<td th:text=""/>
</tr>
</body>
</html>
@Entity
public class ChatUser {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column
private String name;
public ChatUser() {}
public ChatUser(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}