Home > Java > javaTutorial > body text

jsp traverse list collection

(*-*)浩
Release: 2019-05-24 14:38:31
Original
6824 people have browsed it

This article will introduce how to traverse the List collection from a JSP page.

jsp traverse list collection

Example 1:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
    <%
    	List<String> list = new ArrayList<String>();
    	list.add("简单是可靠的先决条件");
    	list.add("兴趣是最好的老师");
    	list.add("知识上的投资总能得到最好的回报");
    	request.setAttribute("list", list);
     %>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Jsp使用c:forEach遍历List集合</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
  </head>
  
  <body>
     <b>遍历List集合的全部元素:</b>
     <br>
     <c:forEach items="${requestScope.list}" var="keyword" varStatus="id">
     	${id.index} ${keyword}<br>
     </c:forEach>
     <br>
     <b>遍历List集合中第一个元素以后的元素(不包括第一个元素):</b>
     <br>
     <c:forEach items="${requestScope.list}" var="keyword" varStatus="id" begin="1">
		${id.index} ${keyword}<br>
     </c:forEach>
  </body>
</html>
Copy after login

Example 2:Two-level List traversal

<%
		List list = new ArrayList();
		List list1 = new ArrayList();
		List list2 = new ArrayList();
		list1.add("1-a");
		list1.add("1-b");
		list2.add("2-d");
		list2.add("2-c");
		list.add(list1);
		list.add(list2);
		request.setAttribute("list1", list1);
		request.setAttribute("list", list);
		%>
		<c:forEach items="${list }" var="item">
			<c:forEach items="${item }" var="item2">
				<tr>
					<td>${item2 }</td>
				</tr>
		</c:forEach>
Copy after login

The above is the detailed content of jsp traverse list collection. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
jsp
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!