EL表达式/JSTL复习总结(2)

<%
	//初始化User
	List<User> users = new ArrayList<User>();
	for (int i = 0; i < 20; i++) {
		User u = new User();
		u.setUsername("baebae_" + i);
		if (i % 2 == 0) {
			u.setGender(EnumSex.M);
		} else {
			u.setGender(EnumSex.F);
		}

		if (i % 3 == 0) {
			u.setDept(new Dept("行政"));
		} else {
			u.setDept(new Dept("IT开发组"));
		}
		users.add(u);
	}
	request.setAttribute("userList", users);
        request.setAttribute("splitString", "ab:bc,cd,de;ef,fg#gh,hi");
%>

<h1>JSTL:</h1>
<table>
	<thead>
		<th width="200">用户名</th>
		<th width="120">性别</th>
		<th width="200">所在部门</th>
	</thead>
	<c:choose>
		<c:when test="${empty userList }">
			<tr>
				<td rowspan="3">用户为空,请检查代码或者数据库</td>
		</c:when>
		<c:otherwise>
			<c:forEach
				var="user"
				items="${userList }"
				varStatus="vs"
			>
				<c:choose>
					<c:when test="${vs.count % 2 ne 0 }">
						<tr bgcolor="#669911">
					</c:when>
					<c:otherwise>
						<tr>
					</c:otherwise>
				</c:choose>
				<td>${user.username }</td>
				<td><c:choose>
						<c:when test="${user.gender eq \"M\" }">
							男
							</c:when>
						<c:when test="${user.gender eq \"F\" }">
							女
							</c:when>
						<c:otherwise>
							不确定
							</c:otherwise>
					</c:choose></td>
				<td>${user.dept.dname }</td>
				</tr>
			</c:forEach>
		</c:otherwise>
	</c:choose>

</table>

 输出结果为:

JSTL

<1>.判断

 

<c:if test="${var}" ></c:if>

<c:choose>

    <c:when test="${var}" var="v">

    </c:when>

    <c:when test="${var}" >

    </c:when>

    <c:otherwise>

    </c:otherwise>

</c:choose>

 

<2>.遍历:

<c:forEach items="${list}" var="item" begin="0" end="${fn:length(userList)" step="1" varStatus="vs"

   

</c:forEach>

<c:forTokens items="${splitString }" delims=",:;#" var="v" >

${v }<br/>

</c:forTokens>

效果:

ab
bc
cd
de
ef
fg
gh
hi

 

你可能感兴趣的:(el表达式)