html - 在 Thymeleaf 中的 For-Each 循环下有一个表单

标签 html spring-mvc spring-boot thymeleaf

我是 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/

相关文章:

spring - 如何使用 Spring MVC "form"标签而不是我的 "input"标签?

java - 如何在 Spring MVC 中填充下拉框

java - Spring Boot——启动时不运行 data.sql

java - Spring Batch-MySQL问题

php - wkhtmltopdf:是否可以合并 PDF 文件?

html - 如何将2个三 Angular 形放在一起css

java - 使用 Spring MVC 在 Java 中编码(marshal) JSON 和泛型

java - 尝试与 azure AuthenticationResult 连接,但出现错误 java.lang.NoClassDefFoundError : com/nimbusds/jwt/JWTParser

html - 为页面内的链接添加边距

c# - 根据客户端不提供aspNetHidden div