JSP中forEach和forTokens循环的用法
<%@page import="java.util.*"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% List<Map<String,String>> list = new ArrayList<Map<String,String>>(); Map<String,String> map = new HashMap<String,String>(); map.put("name", "つかだ"); map.put("ads", "東京"); map.put("phone", "080-1111-1111"); list.add(map); map = new HashMap<String,String>(); map.put("name", "たかはし"); map.put("ads", "千葉"); map.put("phone", "080-1111-2222"); list.add(map); map = new HashMap<String,String>(); map.put("name", "TTT"); map.put("ads", "東京"); map.put("phone", "080-1111-3333"); list.add(map); request.setAttribute("list", list); String[][] ary_2 = new String[3][3]; ary_2[0][0] = "つかだ"; ary_2[0][1] = "東京"; ary_2[0][2] = "080-1111-1111"; ary_2[1][0] = "たかはし"; ary_2[1][1] = "千葉"; ary_2[1][2] = "080-1111-2222"; ary_2[2][0] = "TTT"; ary_2[2][1] = "東京"; ary_2[2][2] = "080-1111-3333"; request.setAttribute("ary_2", ary_2); String info = "つかだ,東京,080-1111-1111;たかはし,千葉,080-1111-2222;TTT,東京,080-1111-3333;"; request.setAttribute("info", info); %> <table border="1"> <tr> <th>名前</th> <th>住所</th> <th>電話NO</th> </tr> <c:forEach items="${ list }" var="map"> <tr> <td>${ map.name }</td> <td>${ map.ads }</td> <td>${ map.phone }</td> </tr> </c:forEach> </table> <br/> <table border="1"> <tr> <th>名前</th> <th>住所</th> <th>電話NO</th> </tr> <c:forEach items="${ ary_2 }" var="ary" varStatus="s"> <tr> <td>${ ary[0] }</td> <td>${ ary[1] }</td> <td>${ ary[2] }</td> </tr> </c:forEach> </table> <br/> <table border="1"> <tr> <th>名前</th> <th>住所</th> <th>電話NO</th> </tr> <c:forTokens items="${ info }" var="one" delims=";" > <tr> <c:forTokens items="${ one }" var="v" delims="," > <td>${ v }</td> </c:forTokens> </tr> </c:forTokens> </table> </body> </html>