List 列表上的迭代
我正在努力使用thymeleaf在列表上循环。 我有两门课:List 列表上的迭代,list,jsp,thymeleaf,List,Jsp,Thymeleaf,我正在努力使用thymeleaf在列表上循环。 我有两门课: public class Contact { private String firstname; private String lastname; private String email; private String phone; public class ContactForm { private List<Contact> contacts; 以及以下观点: <
public class Contact {
private String firstname;
private String lastname;
private String email;
private String phone;
public class ContactForm {
private List<Contact> contacts;
以及以下观点:
<form method="post" action="save.html" th:action="@{/save}"
th:object="${contactForm}" modelattribute="contactForm">
<table>
<c:out value="${contactForm.contacts.size()}" />
<tr th:each="contact : ${contactForm.contacts}">
<td><input type="text" name="field1"
th:field="${contact.firstname}" /></td>
<td><input type="text" name="field2"
th:field="${contact.lastname}" /></td>
<td><input type="text" name="field3 "
th:field="${contact.email}" /></td>
<td><input type="text" name="field4"
th:field="${contact.phone}" /></td>
</tr>
</tbody>
</table>
<br>
<input value="Save" type="submit">
</form>
Application.contact是包含4个元素的联系人列表。
在浏览器上显示该行的结果
<c:out value="${contactForm.contacts.size()}" />
是打印在页面上的“4”,以便将对象和4个元素成功传递到视图。
不幸的是,它从来没有进入th:each,所以里面什么也没有打印出来。
我做错了什么
感谢您抽出时间看来Thymeleaf没有正确配置 使用Spring Boot和Thymeleaf,您的pom中至少需要以下依赖项才能让Spring Boot自动配置Thymeleaf:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动装置
在这个代码段中,缺少了一个,而在你的真实代码中呢?在真实代码中,实际上有一个带有标题和tbody的thead,我刚刚删除了一个较短的代码,而且,我忘了删除结束tbody…我甚至尝试使用这里找到的示例代码我发送了一个视图中包含两个元素的列表,我可以看到列表在视图中有两个项目。但是th:每一个都不起作用。我需要包括一些头文件还是什么?不是Thymeleaf而是带有(Sp)EL的JSP。那么我认为Thymeleaf的设置是正确的。您是否声明了名称空间th(xmlns:th=“)或者适当的JPS标记?您是否配置了Thymeleaf解析程序?您是对的,这是jsp,只是为了强调我的列表已正确发送到视图。我确实声明了th名称空间,但我不知道解析程序。这是我第一次使用Thymeleaf,顺便说一句,这是一个spring启动项目。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>