gpt4 book ai didi

java - 使用 JSP 迭代 Multimap

转载 作者:搜寻专家 更新时间:2023-10-31 19:35:16 25 4
gpt4 key购买 nike

我正在尝试编写一个备份仪表板 来显示多个服务器备份的状态。这个想法是用 JSP 显示一个表,该表在列中具有最近几天的日期,在行中具有服务器名称。在这张穷人的 table 上,我写下了是/否值。

+------------+------------+------------+------------+
+ Host Name | 2011-06-10 | 2011-06-09 | 2011-06-08 |
+------------+------------+------------+------------+
| web01 | Y | Y | N |
+------------+------------+------------+------------+
| web02 | Y | Y | Y |
+------------+------------+------------+------------+

每个服务器都有自己的备份并将状态保存到 Amazon SimpleDb 中,我编写了一个 Java 方法来检索最近几天的此信息,签名如下:

/**
* List MySQL backups of the last howManyDays days. It starts from today
* included at index 0 and goes back in the past until we have a list of
* howManyDays days, even if some day doesn't have any data. Return a list of
* dates, each of which contains a list of backup jobs executed by servers in
* that day.
*
* @param howManyDays
* how many days of backup to show
* @return a Map where each key is the date in ISO format (2011-06-10) and each
* element is a backupJob which is represented by a Map where the key is
* the server name (ex. web01, web01) and the value is "Y" if all was
* fine, otherwise it contains the error message.
*/
public Multimap<String, Map<String, String>> listMysqlBackups(int howManyDays);

Multimap 是 Google Guava Multimap,因为我每天有多个备份。示例输出:

{2011-06-10=[{web06=Y}, {web05=Y}], 2011-06-08=[{web05=Y}, {web06=Y}], 
2011-06-09=[{web05=Y}, {web06=Y}], 2011-06-07=[{web05=Y}, {web06=Y}]}

我不知道如何在 JSP 中使用这些信息。我尝试使用 foreach:

<c:forEach items="${backups}" var="backup" varStatus="backupId">
${backup.key}
</c:forEach>

答案是:

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Don't know 
how to iterate over supplied "items" in <forEach>

现在我在想,如果我用过于复杂的返回值搬起石头砸自己的脚,我是否应该返回一个简单的 HashMap ArrayList,其中每个 HashMap 都包含所有需要的信息(日期、主机名、消息)。如果你们认为这是一种更好的方法,我没有任何问题可以重写提取数据的 Java 方法,但是现在每个单元格都需要遍历所有 ArrayList 来获取元素(这可能没问题,因为 6 服务器乘 7 days 只有 42 个元素)。

您将如何解决这个问题?

最佳答案

JSTL forEach标签不支持 Multimaps .它只能迭代标准集合/映射/数组。

当我需要遍历 Multimap 时在 JSP 中,我使用它的 asMap()看法。这让我可以使用 forEach ,因为它知道如何遍历 Map界面。

它看起来像下面这样:

public Multimap<String, Map<String, String>> listMysqlBackups(int howManyDays) {
// ...
}

public Map<String, Collection<Map<String, String>>> getListMysqlBackupsAsMap() {
return listMysqlBackups(this.numberOfDays).asMap();
}


<c:forEach var="backup" items="${bean.listMysqlBackupsAsMap}">
<c:set var="dateISO" value="${backup.key}/>
<c:set var="backupJobs" value="${backup.value}/> <!-- a Collection<Map<String,String>> -->
<c:forEach var="backupJob" items="${backupJobs}">
<!-- do something with each backup job (Map<String, String>) for the current date -->
</c:forEach>
</c:forEach>

如果您可以使用 JSP EL 2.1,则不需要额外的 getter。您可以简单地调用 asMap()在 JSP 中获取 Map查看。


综上所述,我不确定您是否使用了 Multimap真的在这里做你想做的。 Multimap<String, Map<String, String>>将每个键映射到 Map<String,String>集合 .就您而言,这意味着您拥有:

2011-06-09
--> Collection
--> Map<String, String>
--> Map<String, String>
--> Map<String, String>
2011-06-10
--> Collection
--> Map<String, String>
--> Map<String, String>
--> Map<String, String>

我不确定这就是您想要的。我想你想要一个 Map<String, Map<String, String>> .

另一种解决方案是使用 Guava 的 Table .

关于java - 使用 JSP 迭代 Multimap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6302135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com