我是 Spring Boot 的新手,我对 Thymeleaf 有疑问。我想在 For Each 循环下使用 HTML 表单,但出现异常。
这是我的代码:
<tr th:each="item : ${users}">
<td th:text="${item.getFirstName()}">First Name</td>
<td th:text="${item.getLastName()}">Last Name</td>
<td th:text="${item.getEmailAddress()}">Email Address</td>
<td th:text="${item.getDateOfBirth()}">Date of Birth</td>
<!--<td><a th:href="${'/user/edit/' + user.id}">Edit</a></td> -->
<td>Edit</td>
<td>
<form th:object="${item}" th:action="@{/delete}" method="post">
<input type="hidden" th:field="*{firstName}"/>
<input type="hidden" th:field="*{lastName}"/>
<input type="hidden" th:field="*{emailAddress}"/>
<input type="hidden" th:field="*{dateOfBirth}"/>
<input type="submit" value="Delete"/>
</form>
</td>
</tr>
这是我得到的异常:
[THYMELEAF][http-nio-8080-exec-1] Exception processing template "users": Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor' (users:31)
你能告诉我我做错了什么吗?
提前致谢!!!
最佳答案
我在我的 spring boot 应用程序中使用 thymeleaf 作为 View 引擎。以下示例代码可能会帮助您了解它。
用户模型
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", nullable = false )
private Long id;
@Column(name="user_name" )
private String userName;
@Email(message="Email not valid")
@Column(name="email",nullable=false)
private String email;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
用户 Controller
model.addAttribute("users", userService.getAllusers());
用户 View
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.email}"></td>
<td th:text="${user.userName}"></td>
</tr>
关于html - 在 Thymeleaf 中的 For-Each 循环下有一个表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501010/