gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:41:37 26 4
gpt4 key购买 nike

客户.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";
}

最佳答案

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

这里有一个如何在 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);

关于java - 如何使用thymeleaf在html中传递对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31200779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com