在JSP頁面中使用forEach遍歷List


注意兩點

JSP中引入標簽---c

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

引入Jar包---jstl

這里寫的是在pom.xml中引入

       <dependency>
	  <groupId>jstl</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>
	</dependency>

下面看看一個JSP頁面遍歷List的Demo

<%@ 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>
<table border="1"  width="500" class="table table-striped table-bordered table-hover table-condensed">
	<caption><H3>用戶信息</H3></caption>
	<tr><th>用戶名</th><th>密碼</th></tr>
<%-- 	要在jsp頁面中使用<c>標簽的foreach遍歷,要注意一下兩點:
	1.在jsp開頭引入c標簽
	<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
	2.引入jstl的jar包,可以直接使用lib文件夾也可以使用maven --%>
		<c:forEach items="${userList}" var="user">
			<tr>
				<td>${user.name}</td>
				<td>${user.password}</td>
			</tr>
		</c:forEach>		 
			<!-- 下面的一行是導航條 -->
	<tr><td colspan="5">${pageNav}</td></tr>
</table>
</body>
</html>


免責聲明!

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



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