gpt4 book ai didi

jsp - 在jsp上为arraylist中的项目创建动态表

转载 作者:行者123 更新时间:2023-12-04 14:09:29 25 4
gpt4 key购买 nike

在我的 Spring Web 应用程序中,我想在 JSP 中以表格格式显示模型属性中的 arraylist。我需要动态排列两列中的列表项。下面是只创建一列的代码,但我需要在两列中排列成偶数对。

 <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 , endstep属性代替。您可以让它迭代 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)

关于jsp - 在jsp上为arraylist中的项目创建动态表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6172776/

25 4 0