我目前正在制作一个有8列的表格。首先,我检查值是否为null,如果不为null,我会写出列的列标题(例如:“名字:”)。接下来,我遍历一个列表,并使用适当的值填充每个列(如果它不为null)。我的问题是,当表中的实际数据打印时,列标题(在下面的代码中的注释之间)不会打印。任何人都可以帮助我吗?
<table style="width: 100%">
<c:choose>
<c:when test="${empty serviceRequests}">
</c:when>
<c:otherwise>
<tr> //DOES NOT PRINT STARTING FROM HERE
<td><c:choose>
<c:when test="${empty serviceRequestData['ID']}"></c:when>
<c:otherwise>
<u>ID:</u>
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['FN_Contact']}"></c:when>
<c:otherwise>
<u>First Name:</u>
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['LN_Contact']}"></c:when>
<c:otherwise>
<u>Last Name:</u>
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['Email']}"></c:when>
<c:otherwise>
<u>Email:</u>
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['FN_Reporter']}"></c:when>
<c:otherwise>
<u>Reporter First Name:</u>
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['LN_Reporter']}"></c:when>
<c:otherwise>
<u>Reporter Last Name:</u>
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['Company']}"></c:when>
<c:otherwise>
<u>Company:</u>
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['Notes']}"></c:when>
<c:otherwise>
<u>Notes:</u>
</c:otherwise>
</c:choose></td>
</tr> //DOES NOT PRINT ENDING HERE
<c:forEach var="serviceRequestData" items="${serviceRequests}">
<tr>
<td><c:choose>
<c:when test="${empty serviceRequestData['ID']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['ID']}
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['FN_Contact']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['FN_Contact']}
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['LN_Contact']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['LN_Contact']}
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['Email']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['Email']}
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['FN_Reporter']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['FN_Reporter']}
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['LN_Reporter']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['LN_Reporter']}
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['Company']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['Company']}
</c:otherwise>
</c:choose></td>
<td><c:choose>
<c:when test="${empty serviceRequestData['Notes']}">
Null
</c:when>
<c:otherwise>
${serviceRequestData['Notes']}
</c:otherwise>
</c:choose></td>
</tr>
</c:forEach>
</c:otherwise>
</c:choose>
</table>
答案 0 :(得分:1)
在用于迭代数据行的<c:forEach>
之前,不会定义您在表头中尝试访问的变量。
这应该有效:
<table style="width: 100%">
<c:choose>
<c:when test="${empty serviceRequests}"></c:when>
<c:otherwise>
<tr>
<td>
<u>ID:</u>
</td>
<td>
<u>First Name:</u>
</td>
<td>
<u>Last Name:</u>
</td>
<td>
<u>Email:</u>
</td>
<td>
<u>Reporter First Name:</u>
</td>
<td>
<u>Reporter Last Name:</u>
</td>
<td>
<u>Company:</u>
</td>
<td>
<u>Notes:</u>
</td>
</tr>
<c:forEach var="serviceRequestData" items="${serviceRequests}">
...
</c:forEach>
</c:otherwise>
</c:choose>
</table>
当然,您需要弄清楚要用于包装标头值的逻辑(如果有的话)。