有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java用JSP迭代多重映射

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

+------------+------------+------------+------------+
+ 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数组列表,其中每个HashMap都包含所有需要的信息(日期、主机名、消息)。如果你们认为这是一种更好的方法,我在重写提取数据的Java方法方面没有任何问题,但是现在每个单元格都需要遍历所有ArrayList来获取元素(这可能还可以,因为7天6台服务器只包含42个元素)

你会如何处理这个问题


共 (1) 个答案

  1. # 1 楼答案

    我只想总结一下我所做的事情,但不想说这是我回答问题的最佳解决方案。我很想知道使用google Table collection是否能让事情变得更简单

    将ListMySqlBackup的返回类型更改为简单的HashMap

    /**
     * List the MySQL backups of the last howManyDays days. It starts from today 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 Map with each index as the ISO date 
     * underscore the server name. Key example: 2011-06-11_web01
     * 
     * @param howManyDays
     *         how many days of backup to show
     * @return a Map where each key is the date in ISO format 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 Map<String, String> listMysqlBackups(int howManyDays)  
    

    添加了新方法来返回日期列表和服务器列表

    public static List<String> listDatesFromToday(int howManyDays) {
        List<String> dates = new ArrayList<String>();
        String currentDay = DateHelper.getCurrentDateAsIso();
        while (howManyDays > dates.size()) {
            dates.add(currentDay);
            currentDay = DateHelper.previousDay(currentDay);
        }
        return dates;
    }
    
    public static List<String> listHosts() {
        return ImmutableList.of("web05", "web06");
    }
    

    用嵌套循环显示表格我可以直接搜索钥匙,而无需在地图中搜索,因为我构建钥匙的方式

    <table class="dataTable">
        <tr>
        <th></th>
        <c:forEach items="${days}" var="day">
        <th>${day}${host}</th>
        </c:forEach>
        </tr>
    <c:forEach items="${hosts}" var="host">
        <tr>
        <th>${host}</th>
        <c:forEach items="${days}" var="day">
        <c:set var="key" value="${day}_${host}"/>
        <td> ${backups[key]}  </td>
        </c:forEach>
        </tr>
    </c:forEach>
    </table>
    

    我觉得这个解决方案很简单,我很满意,但如果你们认为Google collection Table可以让代码更简单、更短、更干净,我很乐意听到你们的意见