在JSP中关联两个ArrayList

时间:2014-03-24 08:15:07

标签: java jsp arraylist jstl

我正在尝试关联这两个arraylist并打印..

            <c:when   test="${postitem.posttype.equals('text')}">
                <h5>id="${postitem.postid}"</h5> 
                <pre>  ${postitem.postdata}  </pre>
                <span>datetime="${postitem.posttime}</span>
                <c:forEach items="${postitem.comment}" var="comment">
                    <span>comment="${comment}" </span>
                </c:forEach>
                <c:forEach items="${postitem.commenttime}" var="comment_time">
                    <span>comment_time="${comment_time}"  </span>
                </c:forEach>
            </c:when>

我想用“评论时间”打印“评论”,这是两个不同的arraylists的值,但是这个代码打印整个第一个arraylist然后第二个arraylist。

任何提示???!

1 个答案:

答案 0 :(得分:1)

你必须使用varStatus属性来获取当前索引,并从该索引处的两个arraylists中打印元素(这当然假设两个arraylists包含相同数量的元素):

  
<c:forEach items="${postitem.comment}" var="comment" varStatus="status">
    <span>comment="${comment}" </span>
    <span>comment_time="${postitem.commenttime[status.index]}"  </span>
</c:forEach>