繁体   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