牛骨文教育服务平台(让学习变的简单)
博文笔记

jsp el表达式,for循环

创建时间:2016-05-26 投稿人: 浏览次数:16351
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  3. <%  
  4.     List list = new ArrayList();  
  5.     for (int i = 0; i < 10; i++) {  
  6.         list.add(i);  
  7.     }  
  8.     session.setAttribute("list", list);  
  9. %>  
  10.   
  11. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  12. <html>  
  13.   <head>  
  14.       
  15.     <title>My JSP "index.jsp" starting page</title>  
  16.     <meta http-equiv="pragma" content="no-cache">  
  17.     <meta http-equiv="cache-control" content="no-cache">  
  18.     <meta http-equiv="expires" content="0">      
  19.     <style type="text/css">  
  20.     tr.foreach_tr1{background-color:#D7E9FD;color:black;}  
  21.     tr.foreach_tr2{background-color:#A7DBFF; color:black ;}  
  22.     </style>  
  23.   </head>  
  24.     
  25.   <body>  
  26.     <table border="1" width="200" cellpadding="0" cellspacing="0">  
  27.         <tr align="center"  class="first_tr">  
  28.             <td>  
  29.                 test  
  30.             </td>  
  31.         </tr>  
  32.         <c:forEach items="${list}" varStatus="i" var="item" >  
  33.             <c:if test="${i.index % 2 == 0}" >  
  34.                 <tr align="center" class="foreach_tr1">  
  35.             </c:if>  
  36.             <c:if test="${i.index % 2 == 1}">  
  37.                 <tr align="center" class="foreach_tr2">  
  38.             </c:if>  
  39.             <td>  
  40.                 <h2>${item}</h2>  
  41.             </td>  
  42.         </tr>  
  43.         </c:forEach>  
  44.     </table>  
  45.   </body>  
  46. </html>  

刚刚忘记在文件头上加: <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
结果显示就没有循环出来了...
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。