从 hashmap/arraylist 打印 thymeleaf 中的键/值 [英] print key/value in thymeleaf from hashmap/arraylist
问题描述
java/springboot 中的代码:
@RequestMapping(value = "results")公共字符串结果(模型模型,@RequestParam 字符串搜索类型,@RequestParam String searchTerm) {model.addAttribute("columns", ListController.columnChoices);ArrayList>作业比值 = 作业数据.findforValue(searchTerm);model.addAttribute("items", jobsbyval);返回搜索";}
html/thymeleaf 中的代码:
<表格><tr th:each="item : ${items}"><!--每个循环开始--><td th:text="${item}"></td>//item.value 或 item.key 不起作用!!</tr><!--循环结束--></tbody>这是 html 输出.
{header 1=something, header 2=Analyst, category 3=somename, location=somewhere, Skill=Stats}
所需的表格格式的 HTML 输出(键/值)是:
header 1 东西标题 2 分析师类别somename某处的位置技能统计
解决方案 是的,它不起作用,因为 items
(或 jobsbyval
)不是地图,但它是映射列表,即:ArrayList>作业比值
.
您的 thymeleaf 片段只是打印列表中第一个也是唯一一个地图的字符串表示.如果需要迭代列表中的所有映射,则需要一个嵌套循环,例如:
模型:
List
查看:
<div th:each="mapEntry : ${map}"><span th:text="${mapEntry.key}"></span>=<span th:text="${mapEntry.value}"></span>
输出:
keyinmap1 = valueinmap1keyinmap2 = 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.
这篇关于从 hashmap/arraylist 打印 thymeleaf 中的键/值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆