在我的春季Web应用程序中,我在模型属性中有arraylist,我希望在JSP中以表格格式显示.我需要动态地在两列中排列列表项.下面是只创建一列的代码,但我需要在两列中的偶数对中进行排列.
          <table>
    <c:forEach items="${artifact.answerOptions}" var="answeroption" varStatus="status">
    <tr>
      <td>
      <form:radiobutton path="choosenAnswers" value="${answeroption}"/>
    <label for="choosenAnswers" class="lowerlabel"><c:out value="${answeroption.answerText}"/></label>
      </td>
    </tr>
    </c:forEach>
   </table> 
 这里的answerOptions是具有answerText属性的AnswerOption bean列表.
上面的代码创建表但有一列,但我需要它们以奇怪的方式排列,如下所示:
<table>
  <tr>
     <td> List Item 1</td>
     <td> List Item 2</td>
  </tr>
  <tr>
     <td> List Item 3</td>
     <td> List Item 4</td>
  </tr>
  <tr>
     <td> List Item 5</td>
     <td> List Item 6</td>
  </tr>
</table>
 请改用begin,end和step属性.您可以让它迭代2并直接通过索引获取列表项. 
  
  
 <table>
  <c:forEach begin="0" end="${fn:length(artifact.answerOptions)}" step="2" varStatus="loop">
    <tr>
      <td>${artifact.answerOptions[loop.index]}</td>
      <td>${artifact.answerOptions[loop.index + 1]}</td>
    </tr>
  </c:forEach>
</table> 
 (当你有奇数项目时,不会抛出ArrayIndexOutOfBoundsException)
