171201 BoardList.jsp_pageCount - RYUDONGJIN/Memo_wiki GitHub Wiki

<%@page import="model.BoardBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="model.BoardDAO"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
	request.setCharacterEncoding("UTF-8");
	//๊ฒŒ์‹œ๊ธ€ ๋ณด๊ธฐ์— ์นด์šดํ„ฐ๋ง์„ ์„ค์ •ํ•˜๊ธฐ ์œ„ํ•œ ๋ณ€์ˆ˜๋“ค์„ ์„ ์–ธ 
	//ํ™”๋ฉด์— ๋ณด์—ฌ์งˆ ๊ฒŒ์‹œ๊ธ€์˜ ๊ฐœ์ˆ˜๋ฅผ ์ง€์ •
	int pageSize=10;
	//ํ˜„์žฌ ์นด์šดํ„ฐ๋ฅผ ํด๋ฆญํ•œ ๋ฒˆํ˜ธ๊ฐ’์„ ์ฝ์–ด์˜ด
	String pageNum = request.getParameter("pageNum");
	//๋งŒ์•ฝ ์ฒ˜์Œ boardlist.jsp๋ฅผ ํด๋ฆญํ•˜๊ฑฐ๋‚˜ ์ˆ˜์ •, ์‚ญ์ œ ๋“ฑ ๋‹ค๋ฅธ ๊ฒŒ์‹œ๊ธ€์—์„œ ์ด ํŽ˜์ด์ง€๋กœ ๋„˜์–ด์˜ค๋ฉด 
	//pageNum๊ฐ’์ด ์—†๊ธฐ์— null์ฒ˜๋ฆฌ๋ฅผ ํ•ด์คŒ
	if(pageNum == null){
		pageNum = "1";
	}
	int count = 0; //์ „์ฒด ๊ธ€์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
	int number = 0; //ํŽ˜์ด์ง€ ๋„˜๋ฒ„๋ง ๋ณ€์ˆ˜
	
	//ํ˜„์žฌ ๋ณด๊ณ ์ž ํ•˜๋Š” ํŽ˜์ด์ง€ ์ˆซ์ž๋ฅผ ์ €์žฅ
	int currentPage = Integer.parseInt(pageNum);
	//์ „์ฒด ๊ฒŒ์‹œ๊ธ€์˜ ๋‚ด์šฉ์„ jsp์ชฝ์œผ๋กœ ๊ฐ€์ ธ์™€์•ผ ํ•จ
	BoardDAO bdao = new BoardDAO();
	//์ „์ฒด ๊ฒŒ์‹œ๊ธ€์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ฝ์–ด๋“œ๋ฆฌ๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
	count = bdao.getAllCount();
	////A
	//ํ˜„์žฌ ํŽ˜์ด์ง€์— ๋ณด์—ฌ์ค„ ์‹œ์ž‘ ๋ฒˆํ˜ธ๋ฅผ ์„ค์ • = ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ถˆ๋Ÿฌ์˜ฌ ์‹œ์ž‘๋ฒˆํ˜ธ
	int startRow = (currentPage-1)*pageSize+1;
	int endRow = currentPage * pageSize;
	//์ตœ์‹ ๊ธ€ 10๊ฐœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๊ฒŒ์‹œ๊ธ€์„ ๋ฆฌํ„ด ๋ฐ›์•„์ฃผ๋Š” ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
	////B
	ArrayList<BoardBean> al = bdao.getAllBoard(startRow, endRow);
	//ํ…Œ์ด๋ธ”์— ํ‘œ์‹œํ•  ๋ฒˆํ˜ธ๋ฅผ ์ง€์ •
	number = count - (currentPage -1) * pageSize;
%>
<!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>
	<center>
	<h2>์ „์ฒด ๊ฒŒ์‹œ๊ธ€ ๋ณด๊ธฐ</h2>
		<table width="700" border="1" bgcolor="#7FE5FA">
			<tr height="40">
				<td align="right" colspan="5">
					<input type="button" value="๊ธ€์“ฐ๊ธฐ" onclick="location.href='BoardWriteForm.jsp'">
				</td>
			</tr>
			<tr height="40">
				<td width="50" align="center">๋ฒˆํ˜ธ</td>
				<td width="320" align="center">์ œ๋ชฉ</td>
				<td width="100" align="center">์ž‘์„ฑ์ž</td>
				<td width="150" align="center">์ž‘์„ฑ์ผ</td>
				<td width="80" align="center">์กฐํšŒ์ˆ˜</td>
			</tr>
	<%
		for(int i=0; i<al.size(); i++){
			BoardBean bean = al.get(i); //๋ฆฌ์ŠคํŠธ์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๋นˆ ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜์”ฉ ์ถ”์ถœ
	%>
			<tr height="40">
				<td width="50" align="center"><%=number-- %></td>
				<td width="320" align="left"><a href="BoardInfo.jsp?num=<%=bean.getNum() %>" style="text-decoration:none">
				<%
					if(bean.getRe_step()>1){
						for(int j=0; j<(bean.getRe_step()-1)*5; j++){
				%>&nbsp;			
				<%
						}
					}
				%>
				<%=bean.getSubject() %></a></td>
				<td width="100" align="center"><%= bean.getWriter() %></td>
				<td width="150" align="center"><%= bean.getReg_date() %></td>
				<td width="80" align="center"><%= bean.getReadcount() %></td>
			</tr>
	<%
		}
	%>
		</table>
		<p>
		<!-- ํŽ˜์ด์ง€ ์นด์šดํ„ฐ๋ง ์†Œ์Šค๋ฅผ  ์ž‘์„ฑ -->
		<%
			if(count>0){
				//์นด์šดํ„ฐ๋ง ์ˆซ์ž๋ฅผ ์–ผ๋งˆ๊นŒ์ง€ ๋ณด์—ฌ์ค„๊ฑด์ง€ ๊ฒฐ์ •
				int pageCount = count / pageSize + (count%pageSize == 0 ? 0 : 1);
				//์‹œ์ž‘ํŽ˜์ด์ง€ ์ˆซ์ž๋ฅผ ์„ค์ •
				int startPage = 1;
				if(currentPage % 10 != 0){
					startPage = (int)(currentPage/10)*10+1;
				} else {
					startPage = ((int)(currentPage/10)-1)*10+1;
				}
				int pageBlock = 10;//์นด์šดํ„ฐ๋ง ์ฒ˜๋ฆฌ์ˆซ์ž
				int endPage = startPage+pageBlock-1; //ํ™”๋ฉด์— ๋ณด์—ฌ์งˆ ํŽ˜์ด์ง€์˜ ๋งˆ์ง€๋ง‰ ์ˆซ์ž
				
				if(endPage > pageCount) endPage = pageCount;
				//์ด์ „์ด๋ผ๋Š” ๋งํฌ๋ฅผ ๋งŒ๋“ค๊ฑด์ง€ ํŒŒ์•…
				if(startPage > 10){
		%>
					<a href="BoardList.jsp?pageNum=<%=startPage-10%>">[์ด์ „]</a>
		<%
				}
				//ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ
				for(int i=startPage; i<=endPage; i++){
		%>
					<a href="BoardList.jsp?pageNum=<%=i%>">[<%=i %>]</a>
		<%			
				}
				//๋‹ค์Œ์ด๋ผ๋Š” ๋งํฌ๋ฅผ ๋งŒ๋“ค๊ฑด์ง€ ํŒŒ์•…
				if(endPage < pageCount){
		%>
					<a href="BoardList.jsp?pageNum=<%=startPage+10%>">[๋‹ค์Œ]</a>
		<%			
				}
			}
		%>
		</p>
	</center>
</body>
</html>
page Ex
์ด ๊ฒŒ์‹œ๊ธ€์ด : 184๊ฐœ 
int pageSize=10;
String pageNum = 1
int count = 0; //์ „์ฒด ๊ธ€์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
int number = 184; //ํŽ˜์ด์ง€ ๋„˜๋ฒ„๋ง ๋ณ€์ˆ˜ = count - (currentPage -1) * pageSize;
int currentPage = 1
count = 184
int startRow=1 = (currentPage-1)*pageSize+1;
int endRow=10 = currentPage * pageSize;
int pageCount=19 = count / pageSize + (count%pageSize == 0 ? 0 : 1);
int startPage = 1;
int pageBlock = 10;
int endPage=10 = startPage+pageBlock-1;
1 2 3 4 5 6 7 8 9 10 [๋‹ค์Œ] ๋‹ค์Œ์„ ๋ˆ„๋ฅด๋ฉด startPage=1+10=11 ๋˜์„œ ๋‹ค์‹œ ์ฒ˜์Œ์œผ๋กœ ๋„˜์–ด๊ฐ

์ด ๊ฒŒ์‹œ๊ธ€์ด : 184๊ฐœ 
int pageSize=10;
String pageNum = 11
int count = 0; //์ „์ฒด ๊ธ€์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜
int number = 84; //ํŽ˜์ด์ง€ ๋„˜๋ฒ„๋ง ๋ณ€์ˆ˜ = count - (currentPage -1) * pageSize;
int currentPage = 11
count = 184
int startRow=101 = (currentPage-1)*pageSize+1;
int endRow=110 = currentPage * pageSize;
int pageCount=19 = count / pageSize + (count%pageSize == 0 ? 0 : 1);
int startPage = 11;
int pageBlock = 10;
int endPage=19 = startPage+pageBlock-1;
[์ด์ „] 11 12 13 14 15 16 17 18 19 
โš ๏ธ **GitHub.com Fallback** โš ๏ธ