jstl 遍历数组

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2. pageEncoding="UTF-8"%>  
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  4. <%@ page import="java.util.*"%>  
  5. <%@ page import="bean.User"%>  
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  7.    
  8. <%  
  9.     int[] ages = { 1, 2, 3, 4, 5 }; // 普通数组,JSTL直接使用JSP赋值表达式来取  
  10.    
  11.     List<String> names = new LinkedList<String>(); // List  
  12.     names.add("Biao");  
  13.     names.add("彪");  
  14.     names.add("雷");  
  15.     request.setAttribute("names", names); // 添加到request  
  16.       
  17.     Set<String> set = new TreeSet<String>(); // Set  
  18.     set.add("One");  
  19.     set.add("One");  
  20.     set.add("Two");  
  21.     set.add("Three");  
  22.     set.add("Set");  
  23.       
  24.     Map<String, String> map = new HashMap<String, String>(); // Map  
  25.     map.put("1", "黄彪");  
  26.     map.put("2", "丫头");  
  27.     map.put("3", "哥哥");  
  28.     map.put("4", "笨蛋");  
  29.       
  30.     List<User> users = new ArrayList<User>(); // JavaBean的List  
  31.     users.add(new User("黄彪", "xxxxxx"));  
  32.     users.add(new User("昊天", "xxxxxx"));  
  33.     users.add(new User("姐姐", "yyyyyy"));  
  34.     users.add(new User("丫头", "zzzzzz"));  
  35.     session.setAttribute("users", users); // 添加到session  
  36. %>  
  37. <html>  
  38. <head>  
  39. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  40. <title>Iterator Collections</title>  
  41. <style type="text/css">  
  42. table {  
  43.     border-collapse: collapse;  
  44.     border: 1px solid black;  
  45. }  
  46.    
  47. td, th {  
  48.     border: 1px solid black;  
  49. }  
  50.    
  51. tr:nth-child(even) {  
  52.     background: #eee;  
  53. }  
  54. </style>  
  55. </head>  
  56.    
  57. <body>  
  58. <center>  
  59. 遍历数组:   
  60. <c:forEach var="age" items="<%= ages %>">  
  61. <c:out value="${age}" />  
  62. </c:forEach>  
  63.    
  64. <br />  
  65. 遍历List:  
  66. <c:forEach var="name" items="<%= names %>">  
  67.     <c:out value="${name}" />  
  68. </c:forEach>  
  69.    
  70. <br />  
  71. 遍历List:  
  72. <c:forEach var="name" items="${names}">  
  73.     <c:out value="${name}" />  
  74. </c:forEach>  
  75.    
  76. <br />  
  77.    
  78. <br />  
  79. 遍历Set:  
  80. <c:forEach var="entry" items="<%= set %>">  
  81.     <c:out value="${entry}" />  
  82. </c:forEach>  
  83.    
  84. <br />  
  85. 遍历Map:  
  86. <table>  
  87.     <tr>  
  88.         <th>Key</th>  
  89.         <th>Value</th>  
  90.     </tr>  
  91. <c:forEach var="entry" items="<%= map %>">  
  92. <tr>  
  93. <td><c:out value="${entry.key}"/></td>  
  94. <td><c:out value="${entry.value}"/></td>  
  95. </tr>  
  96. </c:forEach>  
  97. </table>  
  98.    
  99. <br />  
  100. 遍历UserBean的List:  
  101. <table>  
  102.     <tr>  
  103.         <th>username</th>  
  104.         <th>password</th>  
  105.     </tr>  
  106. <c:forEach var="user" items="${users}">  
  107. <tr>  
  108. <td><c:out value="${user.username}"/></td>  
  109. <td><c:out value="${user.password}"/></td>  
  110. </tr>  
  111. </c:forEach>  
  112. </table>  
  113.    
  114. </center>  
  115. </body>  
  116. </html>

猜你喜欢

转载自javatea.iteye.com/blog/1940750