从哈希图/数组列表中打印百里香中的键/值 [英] print key/value in thymeleaf from hashmap/arraylist
问题描述
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";
}
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>
这是html输出.
{header 1=something, header 2=Analyst, category 3=somename, location=somewhere, skill=Stats}
所需的表格格式HTML输出(键/值)为:
header 1 something
header 2 Analyst
category somename
location somewhere
skill Stats
是的,它不起作用,因为items
(或jobsbyval
)不是地图,而是地图列表,即:ArrayList<HashMap<String, String>> jobsbyval
.
您的百里香片段仅打印列表中第一个也是唯一的地图的字符串表示形式.如果需要迭代列表中的所有地图,则需要一个嵌套循环,例如:
模型:
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);
查看:
<div th:each="map : ${mapsList}">
<div th:each="mapEntry : ${map}">
<span th:text="${mapEntry.key}"></span> =
<span th:text="${mapEntry.value}"></span>
</div>
</div>
输出:
keyinmap1 = valueinmap1
keyinmap2 = valueinmap2
th:each
接受地图,在这种情况下:
迭代地图时,迭代变量将属于此类 java.util.Map.Entry
有关详细信息-请参见此处.>
The code in 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:
<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.
{header 1=something, header 2=Analyst, category 3=somename, location=somewhere, skill=Stats}
The desired HTML output(key/value) in table format would be:
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
.
Your thymeleaf snippet just prints the string representation of the first and only map within the list. 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:
When iterating maps, iter variables will be of class java.util.Map.Entry
For details - see here.
这篇关于从哈希图/数组列表中打印百里香中的键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!