简体   繁体   English

如何使用哈希图k,v填充html表?

[英]how to populate html table with hashmap k,v?

I have a hashmap with kv, and there are some dup keys there, like: 我有一个带有kv的哈希表,并且那里有一些dup键,例如:

123 foo 123福

123 goo 123咕

345 ggg 345克

567 kkk 567 kkk

I want to populate my html table with this info, even if there is duplicates, so this is how I could just print it: 我想用此信息填充html表,即使有重复也是如此,因此这就是我可以打印的方式:

for (Map.Entry<String, List<String>> entry : total.entrySet()) {
  for (String s : entry.getValue()) {
    System.out.println(entry.getKey() + " " + s);
  }
}

so now how can I populate the table, i tried something like this: 所以现在我该如何填充表格,我尝试了如下操作:

<table id="ptable" border="1">

            <tr>
                    <td style="text-align: center;">ID</td>
                    <td style="text-align: center;">Month</td>
            </tr>
</table>

and then: 接着:

< c:forEach var="employeeHash" items="${employeeHash}" >
    <td>${employeeSkills.key.id}</td>
</c:forEach>

but I dont know how to get to the value for each key... 但我不知道如何获得每个键的价值...

i want the final res to look like: 我希望最终的res看起来像:

key val 关键值

123 kkk 123千KK

123 fff 123 fff

345 lll 345升

Assuming your employeeObjMap has key and values. 假设您的employeeObjMap具有键和值。 Following code should work. 下面的代码应该工作。

<table>
  <TH>Key</th>
  <TH>Value</th>
  <c:forEach items="${employeeObjMap }" var="current">
    <tr>
      <td><c:out value="${current.key}" /><td>
      <td><c:out value="${current.value}" /><td>
    </tr>
  </c:forEach>
</table>

You can say that you also want the value, like that: 您可以说您也想要该值,如下所示:

<c:forEach var="employeeHash" items="${employeeHash}" >
    <td>${employeeSkills.key.id}</td>
    <td>${employeeSkills.value}</td>
</c:forEach>

Take a look into this link: Use <c:forEach> with HashMap 看一下此链接: 将<c:forEach>与HashMap一起使用

This should be a comment but I do not have enough place. 这应该是一个评论,但我没有足够的位置。 Please do not vote. 请不要投票。

I am not a JSP expert but nesting 2 foreach should do the trick : 我不是JSP专家,但是嵌套2 foreach应该可以解决问题:

<c:forEach var="entry" items="${map}" >
  <!-- entry.key is employee.key -->
  <!-- entry.value is employee.skills -->
  <c:forEach var="skillId" items="${entry.value}" >
    <tr>
      <td>${entry.key}</td>
      <td>${skillId}</td>
    </tr>
  </c:forEach>
</c:forEach>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM