I’m trying to write a backup dashboard showing the status of multiple servers backup. The idea is to show a table with JSP that has the last few days dates in the columns and server names in rows. In this poor man’s table I wrote Yes/No values.
+------------+------------+------------+------------+
+ Host Name | 2011-06-10 | 2011-06-09 | 2011-06-08 |
+------------+------------+------------+------------+
| web01 | Y | Y | N |
+------------+------------+------------+------------+
| web02 | Y | Y | Y |
+------------+------------+------------+------------+
Each server, does its own backup and saves the status into Amazon SimpleDb and I wrote a Java method to retrieve this information of the last 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 the Google Guava Multimap because I’ve multiple backups per day. Example output:
{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 don’t know how to consume 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>
Now I’m thinking if I’m shooting myself in the foot with a too complex return value and whether I should instead return a simple ArrayList of HashMap where each HashMap contains all the needed info (date, hostname, message). If you guys think is a better approach I don’t have any problems to rewrite the Java method extracting the data, but each cell will now require to loop across all the ArrayList to get the element (which could be ok because 6 servers by 7 days is only 42 elements).
How would you approach this problem?
The JSTL
forEachtag does not supportMultimaps. It can only iterate over standard collections / maps / arrays.When I need to iterate over a
Multimapin a JSP, I use itsasMap()view. This lets me useforEach, since it knows how to iterate over theMapinterface.It would look like the following:
If you can use JSP EL 2.1, you do not need the additional getter. You can simply call
asMap()inside the JSP to obtain theMapview.All this being said, I’m not sure your use of a
Multimapreally does what you want here. AMultimap<String, Map<String, String>>maps each key to a collection ofMap<String,String>.In your case, it means that you have:
I’m not sure that’s what you want here. I think you want a
Map<String, Map<String, String>>.Another solution would be to use Guava’s Table.