簡體   English   中英

在Thymeleaf的For-Each循環中擁有表單

[英]Having a form under a For-Each cycle in 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用作視圖引擎,下面的示例代碼可能有助於您了解它。

用戶模型

@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;
    }

}

用戶控制器

model.addAttribute("users", userService.getAllusers());

使用者檢視

<tr th:each="user : ${users}">
    <td th:text="${user.id}"></td>
    <td th:text="${user.email}"></td>
    <td th:text="${user.userName}"></td>
</tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM