有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用thymeleaf在html中传递对象

顾客。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>

控制器。爪哇

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

共 (1) 个答案

  1. # 1 楼答案

    我的第一个问题是,如果这些字符串没有添加到模型中,您需要为它们定义什么

    这里有一个在thymeleaf中迭代列表的示例

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