JSTL foreach及if when标签使用

需要在jsp中加入以下标签库和函数库

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  //必须
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> //需要格式化一些参数时使用,非必须

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>//对数据进行处理时,需要用到的函数,非必须

1.遍历list<对象>

<c:forEach items="${list}" var="list" varStatus="status">    

               ${list.name}      

</c:forEach>

2.遍历list<String>

<c:forEach items="${optList}" var="optList" varStatus="status">    

扫描二维码关注公众号,回复: 794547 查看本文章

               ${optList}      

</c:forEach>

3.遍历list<Map<String,String>>

<c:forEach var="name" items="${a}">
   <c:forEach var="entry" items="${name}">
      <c:out value="${entry.key}"/>
      <c:out value="${entry.value}"/>
    </c:forEach>
</c:forEach>

4.遍历map

<c:forEach items="${map}" var="mymap" >
   <c:out value="${mymap.key}" />
   <c:out value="${
mymap.value}" />
</c:forEach>

5.if使用

首先在jsp中加入


<c:if test="${salary > 2000}">
   <p>My salary is: <c:out value="${salary}"/><p>
</c:if>

6.choose标签<c:choose>
   <c:when test="${param<=2}"> 
            1
   </c:when>

   <c:when test="${param>=2}"> 
            2
   </c:when>
   <c:otherwise>
           ${param}
   </c:otherwise>
</c:choose>

猜你喜欢

转载自lafecat.iteye.com/blog/2229235