forEach use
<% String [] arr = {"yy" ,"qq" ,"ee"}; //Must be set to the domain first, the following EL expression can get this object pageContext.setAttribute("arr", arr); %> <!-- forEach loop--> <c:forEach items="${arr }" var="str"> <c:out value="${str }"></c:out> </c:forEach> <c:out value="${123 }"></c:out> <c:forEach begin="1" end="10" var="ss"> <c:out value="${ss }"></c:out> </c:forEach>
Can traverse collections, list maps, etc.
Another property of JSTL:
The forEach tag also has an attribute: varStatus, which is used to specify the variable name that receives the "loop status", for example: <forEach varStatus="vs" .../>, then you can use the vs variable to get the status of the loop . current item (of the collection) for the current iteration index the iteration index of the current iteration starting at 0 count The iteration count for the current iteration starting at 1 first is used to indicate whether the current iteration is the first iteration last is the flag used to indicate whether the current iteration is the last iteration <c:forEach var="item" items="${ns }" varStatus="vs"> <c:if test="${vs.first }">第一行:</c:if> <c:if test="${vs.last }">最后一行:</c:if> <c:out value="第${vs.count }行: "/> <c:out value="[${vs.index }]: "/> <c:out value="name: ${vs.current }"/><br/> </c:forEach>