我正在尝试在 Spring Boot 应用程序上使用 Thymeleaf 创建我的 Activity 的参加者表。
我的表格是这样的:
<table class="table table-bordered tabl-striped">
<thead class="thead-dark">
<th>First Name</th>
<th>Last Name</th>
</thead>
<tbody>
<tr th:each="tempEvent : ${event}">
<td th:text="${tempEvent.clubEventAttendees[event].firstName}" />
<td th:text="${tempEvent.clubEventAttendees[event].lastName}" />
</tr>
</tbody>
</table>
如果我只将单个 td 与 <td th:text="${tempEvent.clubEventAttendees}" />
一起使用我明白了:
[EventAttendees [id=1, firstName=Paul, lastName=Jones], EventAttendees [id=2, firstName=Luke, lastName=Smyth]
此输出包含正确的数据。然而,上表仅输出我的第二位与会者(Luke Smyth)。如果我将 td 行更改为:
<td th:text="${tempEvent.clubEventAttendees[0].firstName}" />
<td th:text="${tempEvent.clubEventAttendees[0].lastName}" />
或
<td th:text="${tempEvent.clubEventAttendees[1].firstName}" />
<td th:text="${tempEvent.clubEventAttendees[1].lastName}" />
根据位置显示正确的人。如何更改我的表格以显示返回的所有与会者?
这是我的事件模型:
@Entity
@Table(name="club_events")
public class Event {
@OneToMany(mappedBy="clubEvent",
cascade={CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH,
CascadeType.REMOVE})
private List<EventAttendees> clubEventAttendees;
// define fiends
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="location")
private String location;
@Column(name="description")
private String description;
@DateTimeFormat(pattern="yyyy-MM-dd")
@Column(name="date_time")
private LocalDate dateTime;
public Event() {
}
public Event(int id, String name, String location, String description, LocalDate dateTime) {
super();
this.id = id;
this.name = name;
this.location = location;
this.description = description;
this.dateTime = dateTime;
}
}
这是我的 EventAttendees 模型:
@Entity
@Table(name="club_event_attendees")
public class EventAttendees {
@ManyToOne(cascade= {CascadeType.PERSIST, CascadeType.MERGE,
CascadeType.DETACH, CascadeType.REFRESH})
@JoinColumn(name="club_event_id")
private Event clubEvent;
// define fiends
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="phone")
private String phone;
@Column(name="mobile")
private String mobile;
public EventAttendees() {
}
public EventAttendees(int id, String firstName, String lastName, String phone, String mobile, int clubEventId) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.mobile = mobile;
}
}
注意:getter 和 setter 已被删除。
一旦我弄明白了,“与会者”列将被删除,但如果单击“查看与会者”按钮,我想转到一个包含类似表格但包含所有与会者的名字、姓氏、电话和手机的页面.基本上,我想获取 clubEventAttendees 中返回的数据,并将其显示在类似于上表的表格中。
最佳答案
好的,正如所 promise 的,这是代码。
我可以用 Person
来解释这个例子和 Car
.这个想法是Person
有List<Car>
.
按下查看汽车后,将打开一个新页面,您可以在其中看到汽车列表。
实现这一目标的方法相当简单。渲染时 List<Person>
来自模型属性 people
, 你可以使用下面的代码
<tr th:each="person : ${people}">
<td th:text="${person.id}"></td>
<td th:text="${person.firstName}"></td>
<td th:text="${person.lastName}"></td>
<td>
<a th:href="@{'/person/' + ${person.id} + '/cars'}">View Cars</a>
</td>
</tr>
Href 来自 th:href="@{'/person/' + ${person.id} + '/cars'}"
在 Controller 中执行以下方法
@GetMapping("/person/{personId}/cars")
public String getCars(Model model, @PathVariable Integer personId){
model.addAttribute("cars", carRepository.findAllByPersonId(personId));
return "web/car-list";
}
之后呈现一个新模板。我在 GitHub 上上传了源代码,可以在这里找到 https://github.com/Prebiusta/thymeleaf-playground
希望对您有所帮助,如果还有其他问题,请告诉我。
关于java - 在 Thymeleaf 表中显示多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64379806/