iterating of muliple items in jstl

坚强是说给别人听的谎言 提交于 2019-12-30 05:31:04

问题


I have this requirement to iterate over 3 lists at the same time in jstl. for iterating over a single list we use

<c:forEach var = "mfgn" items = "${requestScope.mfgNumber}" varStatus = "status">
    do something;   
</c:forEach>

I need to do some thing like

<c:forEach var = "mfgn" var = "issue" items = "${requestScope.mfgNumber}" items = "${requestScope.something" varStatus = "status">
     mfgNumber;     
</c:forEach>

is this possible or there an otherway to iterate over multiple lists at the same time.


回答1:


If they have the same size, then there are two options, assuming that it are List<Integer> and List<String>:

  1. Merge them in a single list with entities which in turn repesents the items of each other list in a single class like List<ManfacturerIssue> where the ManfacturerIssue is a javabean class which contains Integer number and String issue properties. This way you can end up doing:

    <c:forEach items="${mfgIssues}" var="mfgIssue">
        ${mfgIssue.number}, ${mfgIssue.issue}
    </c:forEach>
    
  2. Iterate by index instead, this is however ugly and unmaintainable as (fill in):

    <c:forEach begin="0" end="${fn:length(mfgNumbers) - 1}" varStatus="loop">
        ${mfgNumbers[loop.index]}, ${issues[loop.index]}
    </c:forEach>
    


来源:https://stackoverflow.com/questions/4821485/iterating-of-muliple-items-in-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!