[英]print key/value in thymeleaf from hashmap/arraylist
The code in java/springboot: java/springboot中的代码:
@RequestMapping(value = "results")
public String results(
Model model,
@RequestParam String searchType,
@RequestParam String searchTerm) {
model.addAttribute("columns", ListController.columnChoices);
ArrayList<HashMap<String, String>> jobsbyval = JobData
.findforValue(searchTerm);
model.addAttribute("items", jobsbyval);
return "search";
}
The code in html/thymeleaf: html/thymeleaf 中的代码:
<div>
<table>
<tbody>
<tr th:each="item : ${items}">
<!--each loop begins -->
<td th:text="${item}"></td> //item.value or item.key dont work!!
</tr>
<!--loop ends -->
</tbody>
</table>
</div>
Here is the html ouput.这是 html 输出。
{header 1=something, header 2=Analyst, category 3=somename, location=somewhere, skill=Stats}
The desired HTML output(key/value) in table format would be:表格格式所需的 HTML 输出(键/值)为:
header 1 something
header 2 Analyst
category somename
location somewhere
skill Stats
Yes, it does not work because items
(or jobsbyval
) is not a map but it is a list of maps, namely: ArrayList<HashMap<String, String>> jobsbyval
.是的,它不起作用,因为
items
(或jobsbyval
)不是地图,而是地图列表,即: ArrayList<HashMap<String, String>> jobsbyval
。
Your thymeleaf snippet just prints the string representation of the first and only map within the list.您的 thymeleaf 片段只是打印列表中第一个也是唯一一个地图的字符串表示。 If you need to iterate all maps within the list you need a nested loop, for example:
如果需要迭代列表中的所有映射,则需要一个嵌套循环,例如:
Model :型号:
List<Map<String, String>> mapsList = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<String, String>();
map1.put("keyinmap1", "valueinmap1");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("keyinmap2", "valueinmap2");
mapsList.add(map1);
mapsList.add(map2);
modelMap.put("mapsList", mapsList);
View :查看:
<div th:each="map : ${mapsList}">
<div th:each="mapEntry : ${map}">
<span th:text="${mapEntry.key}"></span> =
<span th:text="${mapEntry.value}"></span>
</div>
</div>
Output :输出:
keyinmap1 = valueinmap1
keyinmap2 = valueinmap2
th:each
accept maps, in this case: th:each
接受地图,在这种情况下:
When iterating maps, iter variables will be of class java.util.Map.Entry
迭代映射时,iter 变量将属于 java.util.Map.Entry 类
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.