Jsp使用遍歷List集合



   
   
  
  
          
  1. <%@ page language= "java" import= "java.util.*" pageEncoding= "UTF-8"%>
  2. <%@ taglib uri= "http://java.sun.com/jsp/jstl/core" prefix= "c" %>
  3. <%
  4. String path = request.getContextPath();
  5. String basePath = request.getScheme()+ "://"+request.getServerName()+ ":"+request.getServerPort()+path+ "/";
  6. %>
  7. <%
  8. List<String> list = new ArrayList<String>();
  9. list.add( "簡單是可靠的先決條件");
  10. list.add( "興趣是最好的老師");
  11. list.add( "知識上的投資總能得到最好的回報");
  12. request.setAttribute( "list", list);
  13. %>
  14. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  15. <html>
  16. <head>
  17. <base href= "<%=basePath%>">
  18. <title>Jsp使用 c:forEach遍歷List集合</title>
  19. <meta http-equiv= "pragma" content= "no-cache">
  20. <meta http-equiv= "cache-control" content= "no-cache">
  21. <meta http-equiv= "expires" content= "0">
  22. <meta http-equiv= "keywords" content= "keyword1,keyword2,keyword3">
  23. <meta http-equiv= "description" content= "This is my page">
  24. </head>
  25. <body>
  26. <b>遍歷List集合的全部元素:</b>
  27. <br>
  28. <c:forEach items= "${requestScope.list}" var= "keyword" varStatus= "id">
  29. ${id.index} ${keyword}<br>
  30. </c:forEach>
  31. <br>
  32. <b>遍歷List集合中第一個元素以后的元素(不包括第一個元素):</b>
  33. <br>
  34. <c:forEach items= "${requestScope.list}" var= "keyword" varStatus= "id" begin= "1">
  35. ${id.index} ${keyword}<br>
  36. </c:forEach>
  37. </body>
  38. </html>

兩層List遍歷


   
   
  
  
          
  1. <%
  2. List list = new ArrayList();
  3. List list1 = new ArrayList();
  4. List list2 = new ArrayList();
  5. list1.add( "1-a");
  6. list1.add( "1-b");
  7. list2.add( "2-d");
  8. list2.add( "2-c");
  9. list.add(list1);
  10. list.add(list2);
  11. request.setAttribute( "list1", list1);
  12. request.setAttribute( "list", list);
  13. %>
  14. <c:forEach items= "${list }" var= "item">
  15. <c:forEach items= "${item }" var= "item2">
  16. <tr>
  17. <td>${item2 }</td>
  18. </tr>
  19. </c:forEach>



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM