1. 程式人生 > >JAVA Web 基於 c:forEach 標籤的分頁功能(程式碼十分簡短,一看便懂,可直接使用)

JAVA Web 基於 c:forEach 標籤的分頁功能(程式碼十分簡短,一看便懂,可直接使用)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="me.domain.Classes" %>
<%@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>學生資訊管理平臺</title>
</head>
<body class="">
     <!-- 分頁操作-->
    <% 
         ArrayList<Classes> list = (ArrayList<Classes>)request.getSession().getAttribute("clist");  //此處是取出所儲存的資料
         int page_current = 1; //當前頁數
         int page_begin = 0;  //起始點,注意:下標從0開始
         int page_end = 9;   //終點,每頁十條資訊
         int total_count = 0;
         if(list != null)
            total_count = list.size();   //資訊的總量
         int page_total = total_count / 10 + (total_count % 10 != 0 ? 1 : 0);
         if(request.getParameter("begin") != null) {
        	 page_current = Integer.parseInt(request.getParameter("begin"));  //獲取當前頁數
                        }
         page_begin = (page_current - 1) * 10;
         page_end = page_begin + 9 > total_count ? total_count : page_begin + 9;
         request.getSession().setAttribute("page_current", page_current);  //儲存到session中
         request.getSession().setAttribute("page_total", page_total);
    %>

	<!--content-->
	<c:forEach var="p" items="${clist}" begin="<%=page_begin %>" end="<%=page_end %>">
			<tr>
					<td>${p.cid}</td>
					<td>${p.cname}</td>
					<td>${p.grade }</td>
			</tr>
	</c:forEach>
	<c:if test="${sessionScope.page_current != 1 }">
	<li><a href="/StuSys/showAllClasses.jsp?begin=${sessionScope.page_current - 1 }">Prev</a></li>
	</c:if>
	<c:if test="${sessionScope.page_current != sessionScope.page_total }">
	<li><a href="/StuSys/showAllClasses.jsp?begin=${sessionScope.page_current + 1 }">Next</a></li>
	</c:if>
	當前頁數 : ${sessionScope.page_current } / ${sessionScope.page_total }

</body>
</html>