天天看點

【JSTL】core标簽---循環控制

      循環控制主要有兩個标簽:<c:forEach><c:forTokens>。下面一次介紹這兩個标簽的使用。

【c:forEach】

文法格式:

【JSTL】core标簽---循環控制

屬性:

【JSTL】core标簽---循環控制

①用forEach标簽循環周遊實體集合

<h3>采用forEach标簽</h3>
	<table >
		<tr>
			<td>使用者名稱</td>
			<td>年齡</td>
			<td>所屬組</td>
		</tr>
		<c:choose>
			<c:when test="${empty users}">
				<tr>
					<td colspan="3">沒有符合條件的資料</td>
				</tr>
			</c:when>
			<c:otherwise>
				<c:forEach items="${users}" var="user">
					<tr>
						<td>${user.username }</td>
						<td>${user.age }</td>
						<td>${user.group.name }</td>
					</tr>
				</c:forEach>
			</c:otherwise>
		</c:choose>
	</table>	
           
【JSTL】core标簽---循環控制

②forEach循環周遊實體集合,用雙數行變紅(varStatus屬性)

<h3>采用forEach标簽,varstatus</h3><br>
	<table >
		<tr>
			<td>使用者名稱</td>
			<td>年齡</td>
			<td>所屬組</td>
		</tr>
		<c:choose>
			<c:when test="${empty users}">
				<tr>
					<td colspan="3">沒有符合條件的資料</td>
				</tr>
			</c:when>
			<c:otherwise>
				<c:forEach items="${users}" var="user" varStatus="vs">
					<c:choose>
						<c:when test="${vs.count mod 2 == 0}">
							<tr bgcolor="red">
						</c:when>
						<c:otherwise>
							<tr>
						</c:otherwise>
					</c:choose>
								<td>${user.username }</td>
								<td>${user.age }</td>
								<td>${user.group.name }</td>
							</tr>
				</c:forEach>
			</c:otherwise>
		</c:choose>
	</table>
           
【JSTL】core标簽---循環控制

③用forEach循環周遊實體集合,顯示指定條數

<h3>采用forEach标簽,begin,end</h3>
	<table >
		<tr>
			<td>使用者名稱</td>
			<td>年齡</td>
			<td>所屬組</td>
		</tr>
		<c:choose>
			<c:when test="${empty users}">
				<tr>
					<td colspan="3">沒有符合條件的資料</td>
				</tr>
			</c:when>
			<c:otherwise>
				<c:forEach items="${users}" var="user" begin="2" end="8" step="2">
					<tr>
						<td>${user.username }</td>
						<td>${user.age }</td>
						<td>${user.group.name }</td>
					</tr>
				</c:forEach>
			</c:otherwise>
		</c:choose>
	</table>	 
           
【JSTL】core标簽---循環控制

④forEach循環周遊map

<li>示範循環控制标簽:forEach,輸出map</li>
<br>
<c:forEach items="${map}" var="entry">
	${entry.key },${entry.value }<br>
</c:forEach>
           
【JSTL】core标簽---循環控制

【c:forTokens】

文法格式:

【JSTL】core标簽---循環控制

屬性:

【JSTL】core标簽---循環控制

用于輸出有使用特殊符号來分隔内容的資料。

<li>示範循環控制标簽:forTokens</li>
<br>
<c:forTokens items="${strTokens}" delims="#" var="v">
	${v }<br>
</c:forTokens>
           
【JSTL】core标簽---循環控制