开发者

JSTL print array list values in ul - li html tag. Right Algorithm.

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_Na开发者_运维知识库me_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.


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜