天天看点

JSTL与Struts2 标签遍历List、Map例子

JSTL遍历List:

<table>
		<thead>
			<tr>
				<th>编号</th>
				<th>姓名</th>
				<th>密码</th>
				<th>邮箱</th>
			</tr>
		</thead>
		<c:forEach items="${spittterList }" var="list">
			<tr>
				<td>${list.id }</td>
				<td>${list.username }</td>
				<td>${list.password }</td>
				<td>${list.email }</td>
			</tr>
		</c:forEach>
	</table>
	<hr>
           

JSTL遍历Map:

<table>
		<thead>
			<tr>
				<th>编号</th>
				<th>姓名</th>
				<th>密码</th>
				<th>邮箱</th>
			</tr>
		</thead>
		<c:forEach items="${spitterMap }" var="jmap">
			<tr>
				<td>${jmap.value.id }</td>
				<td>${jmap.value.username }</td>
				<td>${jmap.value.password }</td>
				<td>${jmap.value.email }</td>
			</tr>
		</c:forEach>
	</table>
           

Struts遍历List:

注意Struts标签的形式是以#开头的,不同于JSTL的${}形式

<table>
		<thead>
			<tr>
				<th>编号</th>
				<th>姓名</th>
				<th>密码</th>
				<th>邮箱</th>
			</tr>
		</thead>
		<s:iterator value="spittterList" var="list" >
			<tr>
				<td><s:property value="#list.id"/></td>
				<td><s:property value="#list.username"/></td>
				<td><s:property value="#list.password"/></td>
				<td><s:property value="#list.email"/></td>
			</tr>
		</s:iterator>
	</table>
           

Struts遍历Map:

跟JSTL类似访问Map的key时用xx.key,访问value用xx.value,需要对value里的属性访问时用xx.value.xxxx

<table>
		<thead>
			<tr>
				<th>编号</th>
				<th>姓名</th>
				<th>密码</th>
				<th>邮箱</th>
			</tr>
		</thead>
		<s:iterator value="spitterMap" var="map">
			<tr>
				<td><s:property value="#map.value.id"/></td>
				<td><s:property value="#map.value.username"/></td>
				<td><s:property value="#map.value.password"/></td>
				<td><s:property value="#map.value.email"/></td>
			</tr>
		</s:iterator>
	</table>
           

继续阅读