JSTL与EL表达式(为空判断)

JSTL与EL表达式(为空判断)

一、循环遍历集合
 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>
 
 JQuery 清空表单:
               $(':input','#formId')
               .not(':button, :submit, :reset, :hidden')
               .val('')
               .removeAttr('checked')
               .removeAttr('selected');
posted @ 2015-03-16 17:09  godlovelian  阅读(8394)  评论(0编辑  收藏  举报