페이징 처리 로직

 

Example

페이지당 게시글 10개

전체 데이터 개수 965개

-> 전체 페이지 개수는 97개

페이지당 게시글 10개

전체 데이터 개수 960개

-> 전체 페이지 개수는 96개

 

즉, (전체 데이터 개수 / 페이지당 게시글) 에서 소수점이하가 없으면 ( 나머지가 없으면) 몫만큼의 개수가 전체페이지 개수이고, 그렇지 않을경우는 +1을 해줘야 한다.

 

전체데이터개수/페이지당게시글 + (전체데이터개수%페이지당게시글== 0 ? 0 : 1);

// 자바에서 int는 정수형만 취급하므로 나누었을 때 소수가 표현되지 않음.

 

1페이지에는 1~10까지의 자료가 나온다.

2페이지는 11~20

3페이지는 21~30

:

n페이지 1+(n-1*10)~n*10 

 

package com.util;

public class MyUtil {
	/**
	 * 전체 페이지수 구하기
	 * @param rows			한화면에 출력할 목록 수
	 * @param dataCount		총 데이터 수
	 * @return				전체 페이지 수
	 */
	public int pageCount(int rows, int dataCount) {
		if(dataCount <= 0) {
			return 0;
		}
		
		return dataCount / rows +(dataCount % rows > 0 ? 1 : 0);
	}
	
	/**
	 * 페이징 처리(GET 방식)
	 * @param current_page	현재 표시되는 페이지 번호
	 * @param total_page	전체 페이지 수
	 * @param list_url		링크를 설정할 주소
	 * @return				페이지 처리 결과
	 */
	public String paging(int current_page, int total_page, String list_url) {
		StringBuilder sb = new StringBuilder();
		
		int numPerBlock = 10;
		int currentPageSetup;
		int n, page;
		
		if(current_page < 1 || total_page < 1) {
			return "";
		} // 데이터가 없으면 설정할 필요가 없음
		
		if(list_url.indexOf("?") != -1) {
			list_url += "&";
		} else {
			list_url += "?";
		}
		
		// currentPageSetup : 표시할 첫 페이지-1
		currentPageSetup = (current_page / numPerBlock) * numPerBlock;
		if(current_page % numPerBlock == 0) {
			currentPageSetup = currentPageSetup - numPerBlock;
		}
		
		sb.append("<div class='paginate'>");
		
		// 처음페이지, 이전(10페이지 전)
		n = current_page - numPerBlock;
		if(total_page > numPerBlock && currentPageSetup > 0) {
			sb.append("<a href='"+list_url+"page=1'>처음</a>");
			sb.append("<a href='"+list_url+"page="+n+"'>이전</a>");
		}
		
		// 페이징 처리
		page = currentPageSetup + 1;
		while(page <= total_page && page <= (currentPageSetup+numPerBlock)) {
			if(page == current_page) {
				sb.append("<span>"+page+"</span>");
			} else {
				sb.append("<a href='"+list_url+"page="+page+"'>"+page+"</a>");				
			}
			page++;
		}
		
		// 다음(10페이지 후), 마지막페이지
		n = current_page + numPerBlock;
		if(n > total_page) n = total_page;
		if(total_page - currentPageSetup > numPerBlock) {
			sb.append("<a href='"+list_url+"page="+n+"'>다음</a>");
			sb.append("<a href='"+list_url+"page="+total_page+"'>끝</a>");
		}
		
		sb.append("</div>");
		
		return sb.toString();
	}
	
}

 

 

테스트

더보기
<%@page import="com.util.MyUtil"%>
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	request.setCharacterEncoding("utf-8");

	// 파라미터로 넘어온 페이지번호(JSP에서 page는 예약어로 변수명으로 사용 불가)
	String pageNum = request.getParameter("page");
	int current_page = 1;
	if(pageNum != null) {
		current_page = Integer.parseInt(pageNum);
	}
	
	MyUtil util = new MyUtil();
	
	int dataCount = 965;
	int rows = 10;
	int total_page = util.pageCount(rows, dataCount);
	if(current_page > total_page) {
		current_page = total_page;
	}
	
	String listUrl = "list.jsp";
	String paging = util.paging(current_page, total_page, listUrl);
	
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<style type="text/css">
* {
	margin: 0; padding: 0;
	box-sizing: border-box;
}

body {
	font-size: 14px;
	font-family: 맑은 고딕, 나눔고딕, 돋움, sans-serif;
}

.paginate {
	text-align: center;
	font-size: 14px;
}

.paginate a {
	border: 1px solid #ccc;
	color: #000;
	font-weight: 600;
	text-decoration: none;
	padding: 3px 7px;
	margin-left: 3px;
	vertical-align: middle;
}

.paginate a:hover, .paginate a:active {
	color: #6771ff;
}

.paginate span {
	border: 1px solid #e28d8d;
	color: #cb3536;
	font-weight: 600;
	padding: 3px 7px;
	margin-left: 3px;
	vertical-align: middle;
}

.paginate :first-child {
	margin-left: 0;
}

.container {
	width: 700px;
	margin: 30px auto;
}

</style>


</head>
<body>

<div class="container">
	<h3>페이징 처리 테스트</h3>
	
	<div style="padding-top: 10px;">
		<%= paging %>
	</div>
</div>


</body>
</html>

+ Recent posts