EL表达式

在jsp页面中不能通过${list.size}取列表长度,而是 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 

list的长度是:${fn:length(list)}


关键在于<c:forEach>的varStatus属性,具体代码如下:

<table width="500" border="0" cellspacing="0" cellpadding="0">
<tr>
<th>序号</th>
<th>姓名</th>
</tr>
<c:forEach var="student" items="${ students}" varStatus="status">
<tr>
<td>${ status.index + 1}</td>
<td>${ student.name}</td>
</tr>
</c:forEach>
</table>

备注:status.index是从0开始的。

 

1、在jsp中引入标准函数声明
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

2、若要判断集合的大小,则需要引入如下声明
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

3、如何使用jstl判断集合是否为空
${user}为集合,user为集合名
<c:if test="${empty user}">无信息!</c:if>为空
<c:if test="${!empty user}">其它</c:if>非空

4、如何取得集合的大小
${fn:length(集合名<如果是session中的集合则应该加上sessionScope.键>)}
${fn:length(map)}

5、如何取得保存在session中的对象?
${sessionScope.键.对象属性}

6、varStatus显示循环变量的状态
例:<tag:forEach var="currentFood" items="${sessionScope.foods}" varStatus="status"
<tag:if test="${status%2==1}">
.....................
</tag:if>
</tag:forEach>
其中status这个变量就记录了循环变量的状态

7、如何遍历Map集合
<c:forEach var="foodmap" items="${sessionScope.cart}" varStatus="status">
<c:set var="subtotal" value="${foodmap.value.bean.foodPrice*foodmap.value.quantity}"></c:set>
</c:forEach>
遍历Map集合的值:
foodmap:保存session中的map
foodmap.value:取得map的值,即获取保存在map中的一个对象
要获取对象中的数据,必须用foodmap.value.quantity去点对象的属性(quantity就是对象的属性)
8、对象属性为空显示默认值
<c:forEach var="customer" items="${customers}">
<tr>
<td><c:out value="${customer.lastName}"/></td>
<td><c:out value="${customer.phoneHome}" default="no home phone specified"/></td>
<td>
<c:out value="${customer.phoneCell}" escapeXml="false">
<font color="red">no cell phone specified</font>
</c:out>
</td>
</tr>
</c:forEach>

<c:set var="stringLength3" value="${fn:length(p.cafName[3])}"></c:set>
业务方案说明书:<a href="<%=path%>/upload/${p.cpId}/${p.aId}/${p.cafName[3]}" target="view_window">${fn:substring(p.cafName[3], 15,stringLength3)}</a>

 
原文地址:https://www.cnblogs.com/zhao-shan/p/6065164.html