簡體   English   中英

如何使用百里香在html中傳遞對象

[英]how to pass the object in html using thymeleaf

customer.html

<div class="table-responsive"> 
    <form data-toggle="validator" role="form" th:action="@{/customers}"          method="post" th:object="${user}">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Row</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Create Date</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                 <tr th:action="@{/customers}" method="post">
                     <td th:value="${info.firstName}">firstName</td>
                     <td th:value="${info.lastName}">lastName</td>
                     <td th:value="${info.createdate}">createdate</td>
                     <td th:value="${info.emailaddress}">emailaddress</td>
                 </tr>
            </tbody>
        </table>
</form>
</div>
</div>

controller.java

@RequestMapping(value = "/customers", method = RequestMethod.GET)
public String customers(Model model) throws Exception {
    String firstName = "prasoon";
    String lastName = "gupta";
    String createdate = "2 july";
    String emailaddress = "prasoon.gupta@abc.com";
    List<Customers> info = scheduleService.getCustomer();
    model.addAttribute("info", info);
    return "pages/customers";
}

我的第一個問題是,如果未將所有這些String添加到模型中,那么您需要定義什么?

這里有一個如何在百里香中迭代列表的示例

         <table class="info table table-hover">
                    <tbody>
                    <tr th:each="customers : ${info}">

                        <td width="20%" th:text="${customers.id}"></td>
                    </tr>
                    </tbody>
                </table>

關於您剛剛問我的問題,我認為您不太了解自己的體系結構。 您希望在信息列表中收到什么?

要在列表中添加額外的客戶,因此必須在模型中執行此操作,但是我仍然認為這是一種糟糕的方法

        String firstName = "prasoon";
        String lastName = "gupta";
        String createdate = "2 july";
        String emailaddress = "prasoon.gupta@abc.com";
        Customers customers = new Customers();
        customers.setFirstName(firstName);
        customers.setLastName(lastName);
        customers.setCreatedate(createdate);
        customers.setEmailaddress(emailaddress);

        List<Customers> info = scheduleService.getCustomer();
        info.add(customers);

暫無
暫無

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

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