JSTL:如何查找对象是否是集合

时间:2014-11-19 06:44:40

标签: java jsp collections jstl

使用this帖子我知道如果一个集合只有1个值而且是字符串,那么它将作为String而不是Collection Object返回。我的问题是我的集合可以包含从Integer到BigDecimal的任何内容,以及集合中返回的String。如果我使用低于JSTL条件,那么它只捕获String类型数据。其他类型呢?

此处queryResults包含对象数组列表:List<Object[]>。因此bookmark应该是对象数组:Object[]

        <c:forEach var="bookmark" items="${queryResults}" varStatus="loopStatus">
            <tr class="${loopStatus.index % 2 == 0 ? 'roweven' : 'rowodd'}">
            <c:choose>
            <c:when test="${bookmark.getClass().simpleName == 'String'}">
             <!--The collection has only 1 record and that is of type String. Can't run for loop. -->
                <td><c:out value="${bookmark}"/></td>
            </c:when>
            <c:otherwise>
                <!--The collection has more than 1 record therefore running a for loop. -->
                <c:forEach begin="0" end="${fn:length(bookmark)-1}" varStatus="loop" >
                    <td><c:out value="${bookmark[loop.count-1]}"/></td>
                </c:forEach>
            </c:otherwise>
            </c:choose>
            </tr>
        </c:forEach>

1 个答案:

答案 0 :(得分:0)

以下应该按您想要的方式工作:

    <c:forEach var="bookmark" items="${queryResults}" varStatus="loopStatus">
        <tr class="${loopStatus.index % 2 == 0 ? 'roweven' : 'rowodd'}">
            <c:forEach var="bookmarkColumn" items="${bookmark}" >
                <td><c:out value="${bookmarkColumn}"/></td>
            </c:forEach>
        </tr>
    </c:forEach>