简体   繁体   中英

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

My first question would be for what for you need define all those String if they´re not added into model?.

Here you have an example of how iterate a list in thymeleaf

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

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

About what you just ask me, I think you dont understand very good your own architecture. What do you expect receive on your info list?

To add an extra customers on your list hence in your model you have to do this, but I still think is a poor technique

        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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM