Iteration of a multimap with JSP

I am trying to write a backup panel showing the backup status of several servers. The idea is to show a table with a JSP that has the dates of the last few days in columns and server names in rows. In this poor person table, I wrote "Yes / No."

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

Each server makes its own backup and stores the status in Amazon SimpleDb, and I wrote a Java method to get this information over the past few days with the following signature:

/**
 * 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 is Google Guava Multimap because I have several backups per day. Output Example:

{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}]} 

I do not know how to use this information in JSP. I tried with foreach:

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

And the answer was:

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

, ArrayList HashMap, HashMap (, , ). , , , , , Java, , ArrayList ( , 6 7 - 42 ).

?

+5
3

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, . asMap() JSP, Map.


, , Multimap , . A 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.

+8

, , , . , Google Table .

listMysqlBackups 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 , , .

+1

,

.

<c:forEach items="${webs}" var="web" varStatus="webId">
    <c:forEach items="${web.backups}" var="backup" varStatus="backupId">
        ${backup.key}
    </c:forEach>
</c:forEach>
-1

All Articles