[英]Iterating over the list collection in thymeleaf
我正在嘗試創建一個簡單的列表,該列表返回List集合中控制器的結果。 以前,我逐一列出列表中的每個結果,但是我知道根據DRY原理,這是一種不好的做法。 這就是為什么我要更改它。 我不是百里香的朋友。
我在乎:
直到列表不為空,我才像以前一樣創建了帶有結果的表。 這次只有循環形式。
那么可以選擇一個表,然后將其保存在存儲庫中,該怎么辦? 在每個表格下使用提交?
請幫忙。 謝謝
<!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>
控制器:
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
上的命名是向后的。 您應該經過多個航班,並且僅提及一個航班:
控制器:
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
的屬性,則可以在Thymeleaf中使用內部循環對routes
進行迭代。 否則,您將向模型添加單獨的列表,並且只需在每個模型上調用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>
常規調試技巧:首先從起作用的東西開始,然后逐步添加。 如果某個東西壞了,您迷路了,請拿走一切,直到一切正常,然后逐漸重新添加,直到找到罪魁禍首。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.