问题
in a JSP I should print the values of an array list in the "li" html tags. The problem is that I should print in one cycle two values. This is the example in html:
<ul class="myProfileTeamNameList">
<li><p class="first">- Team_Name_1</p><p>- Team_Name_2</p></li>
</ul>
I have implemented this but I can only print the first value. This is my code:
<ul class="myProfileTeamNameList">
<c:forEach var="team" items="${teams}">
<li><p class="first">- ${team.name}</p> <p>- ${team.name}</p></li>
</c:forEach>
</ul>
instead in the second
html tag I should write the SUCCESSIVE array list value. Something like: ${team.name} + 1
Can someone help me? Thanks a lot.
回答1:
Ideally, you should not use a list. You should use a Map, and loop through its entries to get the key and value.
But if you really need to use the list, <c:forEach> allows you to write a index-based loop. Instead of items
, specify step=2
, begin
, end
and varStatus
and then refer to ${items[varStatus.index]}
(and .index+1
respectively). E.g.
<c:forEach step="2" being="0" end="${fn:length(array)}" varStatus="status">
${items[varStatus.index]} - ${items[varStatus.index+1]}
</c:forEach>
回答2:
I think the best way to achieve what you want is to pass to the jsp view a list of couple. I mean you could build a teamsInPair list of beans containing the two elements of team. This kind of choice could simplify your business logic in the view.
来源:https://stackoverflow.com/questions/6873459/jstl-print-array-list-values-in-ul-li-html-tag-right-algorithm