我正在尝试创建一个简单的列表,该列表从列表集合中的 Controller 返回结果。 以前,我将列表中的每个结果一一写下来,但我知道根据 DRY 原则,这是一种不好的做法。这就是为什么我正在努力改变它。 我和百里叶不是 friend 。
我关心的是:
直到列表不为空为止,我创建了包含结果的表格,就像以前一样。只是这一次以循环的形式。
那么可以选择一个表并将其保存在存储库中,我应该怎么做?在每个表下使用提交?
请帮忙。 谢谢
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<title>Bootstrap Example</title>
<html xmlns:th="http://www.thymeleaf.org">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css"
href="css/font.css"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script src="js/chart.js"></script>
</head>
<body>
<div class="container">
<h2>Cheapest Flight</h2>
<div class="list-group">
<a href="#" class="list-group-item ">
<b><img th:src="@{'http://pics.avs.io/400/400/' + ${AirlineIataFirst} + '.png'}"></b>
Bad code /////......
<c><p th:text="'Airline: ' + ${AirlineFirst}"/></c>
<d><p th:text="'Price: ' + ${PriceFirst}+ ${Currency}"/></d>
<e><p th:text="'Departure: ' + ${DepartureFirst} "/></e>
<f><p th:text="'Return: ' + ${ReturnFirst} "/></f>
</a>
我正在尝试做的事情:
<a href="#" class="list-group-item" th:if="${flight != null}" >
<b><img th:src="@{'http://pics.avs.io/400/400/' + ${AirlineIataFourth} + '.png'}"></b>
<li th:each="flights : ${flight}" th:text="${flight}"></li>
<li th:each="prices : ${price}" th:text="${price}"></li>
<c><p th:text="'Airline: ' + ${flight}"/></c>
<d><p th:text="'Price: ' + ${price}+ ${Currency}"/></d>
<e><p th:text="'Departure: ' + ${DepartureFourth} "/></e>
<f><p th:text="'Return: ' + ${ReturnFourth} "/></f>
Controller :
model.addAttribute("Currency", flightDTO.getCurrency());
model.addAttribute("flight", cheapFlyService.flightList(output));
model.addAttribute("number", cheapFlyService.flightNumberList(output));
model.addAttribute("return", cheapFlyService.returnAtList(output));
model.addAttribute("departure", cheapFlyService.departureAtList(output));
model.addAttribute("price", cheapFlyService.priceList(output));
最佳答案
在第二种情况下,它似乎不是有效的 HTML 标记。您的<a>
永远不会关闭。另外,您应该避免使用类似 <b>
之类的东西。因为它非常令人困惑(这是一种已弃用的粗体文本方式)。删除所有这些类型的标签,因为它不是有效的 HTML 或者只是令人困惑。
此外,虽然它可以工作,但 th:each
上的命名是向后的。您应该经过多个航类,并仅引用一个航类:
Controller :
model.addAttribute("currency", flightDTO.getCurrency());
model.addAttribute("flights", cheapFlyService.flightList(output));
model.addAttribute("numbers", cheapFlyService.flightNumberList(output));
model.addAttribute("returns", cheapFlyService.returnAtList(output));
model.addAttribute("departures", cheapFlyService.departureAtList(output));
model.addAttribute("prices", cheapFlyService.priceList(output));
HTML:
<th:block th:each="flight : ${flights}">
<span th:text=${flight.number}" th:remove="tag">[Flight Number]</span> <!-- or whatever property is that the Flight class may have. Note the scoping. -->
</th:block>
但主要的反馈是完整的 HTML 将取决于您如何对数据进行建模。例如,如果航类具有名为 routes
的属性,然后您可以迭代 routes
在 Thymeleaf 中有一个内循环。否则,您将向模型添加单独的列表,并且只需调用 toString
每个方法:
<th:block th:each="departure : ${departures}">
<span th:text=${departure}" th:remove="tag">[This is calling toString() on the objects in the list]</span>
</th:block>
一般调试技巧:从有效的东西开始,然后逐渐添加。如果某些东西坏了并且您迷路了,请拿走所有东西直到它可以工作并逐渐重新添加回来,直到找到罪魁祸首。
关于java - 迭代 thymeleaf 中的列表集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56921378/