我正在开发一个员工调度程序Java Web应用程序,员工可以指定他们将在办公室外休假,商务旅行等等的日子......我有一个简单的机制来添加/编辑/删除这些记录。但是我正在努力使用JSTL以及哪个收集器应该传递给jsp用于forEach循环。
最终我需要在jsp页面上显示一个看起来像这样的表:
October 2010
Employee Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu ...
1 2 3 4 5 6 7 ...
Abate * * * * * [E]
Adams * * * * * [E] [E]
Benson * * * * * [E] [E]
如果[E]是该特定日期针对该员工的项目或事件列表
这是来自sql调用的事件的示例列表:
EVENT_ID BADGE EVENT_TYPE_ID EVENT_TYPE_NAME EVENT_S_DATE
1 134311 5610 Business Travel 2010-10-08
2 101379 7646 Floating Holiday 2010-10-11
3 005396 3600 Vacation 2010-10-12
4 134311 1318 Military 2010-10-12
5 134311 0575 Sick Time 2010-10-12
6 101379 6652 Unpaid 2010-10-18
7 111243 0575 Sick Time 2010-10-29
我的第一种方法是使用嵌套的哈希映射并在当天键入它,但我很难使用JSTL引用地图中的值,因为使用数字键的问题(我的键是在开始日偏移的单元格号) ,10月从第5天或周五开始。)
EL access a map value by Integer key
我预见我会在servlet中创建一堆循环来填充某种类型的容器来完成此任务。
我正在寻找至少类似“哦,在地图中嵌入一个列表并将其放入另一个地图中”的内容,以便让我开始。
答案 0 :(得分:0)
只需更改您的DAO方法即可返回Map<Long, Event>
而不是Map<Integer, Event>
。
答案 1 :(得分:0)
好的,我终于想出了嵌套的集合并自行循环,所以我会回答我自己的问题。 servlet和视图代码可能都可以使用一些改进,但它可以工作。
的Servlet
ArrayList eventListPerDayPerBadge = new ArrayList();
ArrayList eventListPerDay = new ArrayList();
HashMap eventMapByDay = new HashMap();
HashMap eventMapByDayByBadge = new HashMap();
int lastDay = 0;
int eventDay = 1;
String lastBadge = "0";
String currentBadge = "currentBadge";
Event event = null;
Iterator eventList = employeeEvents.iterator();
while (eventList.hasNext()) {
event = (Event) eventList.next();
eventDay = DateUtil.toCalendar(event.getEvent_s_date()).get(Calendar.DATE);
currentBadge = event.getBadge();
if (eventDay == lastDay) {
eventListPerDay.add(event);
if (currentBadge.equals(lastBadge)) {
eventListPerDayPerBadge.add(event);
}
else {
HashMap tempMap = new HashMap();
if (eventMapByDayByBadge.get(lastBadge) != null) {
tempMap = (HashMap) eventMapByDayByBadge.get(lastBadge);
}
tempMap.put(new Long(eventDay), eventListPerDayPerBadge);
eventMapByDayByBadge.put(lastBadge, tempMap);
eventListPerDayPerBadge = new ArrayList();
eventListPerDayPerBadge.add(event);
}
eventMapByDay.put(new Long(eventDay), eventListPerDay);
}
else {
if (!"0".equals(lastBadge)) {
HashMap tempMap = new HashMap();
if (eventMapByDayByBadge.get(lastBadge) != null) {
tempMap = (HashMap) eventMapByDayByBadge.get(lastBadge);
}
tempMap.put(new Long(lastDay), eventListPerDayPerBadge);
eventMapByDayByBadge.put(lastBadge, tempMap);
eventMapByDay.put(new Long(lastDay), eventListPerDay);
}
eventListPerDayPerBadge = new ArrayList();
eventListPerDayPerBadge.add(event);
eventListPerDay = new ArrayList();
eventListPerDay.add (event);
}
lastDay = eventDay;
lastBadge = currentBadge;
}
// Get the last record into the maps
HashMap tempMap = new HashMap();
if (eventMapByDayByBadge.get(lastBadge) != null) {
tempMap = (HashMap) eventMapByDayByBadge.get(lastBadge);
}
tempMap.put(new Long(eventDay), eventListPerDayPerBadge);
eventMapByDayByBadge.put(lastBadge, tempMap);
eventMapByDay.put(new Long(eventDay), eventListPerDay);
request.setAttribute("eventMapByDayByBadge", eventMapByDayByBadge);
request.setAttribute("eventMapByDay", eventMapByDay);
视图(并非视图中使用的所有属性都显示在servlet中):
<table align="center" border="1" cellpadding="3" cellspacing="0" width="100%">
<tbody>
<!-- Header ROW -->
<tr>
<th>Employee</th>
<c:forEach var="week" begin="1" end="${iTotalweeks}" varStatus="status">
<th class="optionYellow">Sun</th>
<th>Mon</th>
<th>Tue</th>
<th>Wed</th>
<th>Thu</th>
<th>Fri</th>
<th class="optionYellow">Sat</th>
</c:forEach>
</tr>
<!-- Week Day Number ROW -->
<tr>
<td align="center" height="35" class="dsb">* </td>
<c:forEach var="week" begin="1" end="${iTotalweeks}" varStatus="status">
<c:forEach var="cell" begin="${1+7*(week-1)}" end="${7+7*(week-1)}"
step="1" varStatus="status">
<c:set var="dayNo" value="${cell-weekStartDay+1}" />
<c:choose>
<c:when test="${weekStartDay>cell || (cell-weekStartDay+1)>days}">
<td align="center" height="35" class="<c:out value="${weekendCellMap[cell]}" />">* </td>
</c:when>
<c:otherwise>
<td class="<c:out value="${holidayColorMap[dayNo]}" />"><span class="calDayNo"> <c:out value="${dayNo}" /></span>
<span class="calDayName"> <c:out value="${holidayNameMap[dayNo]}" /></span>
</td>
</c:otherwise>
</c:choose>
</c:forEach>
</c:forEach>
</tr>
<!-- Employee ROW -->
<c:forEach var="employee" items="${employeeList}">
<c:set var="map" value="${eventMapByDayByBadge[employee.badge]}" />
<tr>
<td align="center" height="35" NOWRAP><c:out value="${employee.lname}" />, <c:out value="${employee.firstName}" /><br>
(<c:out value="${employee.badge}" />)
</td>
<c:forEach var="week" begin="1" end="${iTotalweeks}" varStatus="status">
<c:forEach var="cell" begin="${1+7*(week-1)}" end="${7+7*(week-1)}"
step="1" varStatus="status">
<c:set var="dayNo" value="${cell-weekStartDay+1}" />
<c:choose>
<c:when test="${weekStartDay>cell || (cell-weekStartDay+1)>days}">
<td align="center" height="35" class="<c:out value="${weekendCellMap[cell]}" />">* </td>
</c:when>
<c:otherwise>
<td align="center" height="35" class="<c:out value="${holidayColorMap[dayNo]}" />"><span>
<c:forEach var="event" items="${map[dayNo]}">
<c:out value="${event.event_type_name}" />
</c:forEach></span></td>
</c:otherwise>
</c:choose>
</c:forEach>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
视图的输出示例