프로그래밍

[Web] day49 : 페이지네이션 본문

Web/Web

[Web] day49 : 페이지네이션

시케 2023. 8. 7. 00:55
728x90
반응형

2023.07.18.화

페이지네이션

페이지네이션은 데이터양이 많을때 한 화면에서 보여주는 데이터의 범위를 결정하는원하는 만큼 자르는 로직이다

※ 페이징 처리는 DBMS SQL 문에도 존재한다

 

페이징처리

Controller에서 보여줄 데이터의 범위를 정하여 Model에 전달하면 원하는 범위의 데이터만큼 자를 수 있다

package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BoardSet;
import model.BoardSetDAO;

public class MainAction implements Action {

	@Override
	public ActionForward execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ActionForward forward = new ActionForward();
		forward.setPath("main.jsp");
		forward.setRedirect(false);
		
		String count=request.getParameter("count");
		if(count == null || count.isEmpty() || count.isBlank() || count.equals("")) {
			count="2";
		}
        // count == null 이 가장 앞에 있는 이유는 가장 많은 경우 null
        // 뒤의 연산을 최대한 생략하기 위해
        
		int cnt=Integer.parseInt(count);
		
		BoardSetDAO bsDAO=new BoardSetDAO();
		ArrayList<BoardSet> datas=bsDAO.selectAll(null, cnt);
		request.setAttribute("datas", datas);
		request.setAttribute("count", cnt);
		
		return forward;
	}

}
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class BoardSetDAO {
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	static final private String SQL_SELECTALL="SELECT * FROM BOARD ORDER BY BID DESC LIMIT 0,?";
	static final private String SQL_SELECTALL_REPLY="SELECT * FROM REPLY WHERE BID=? ORDER BY RID DESC";
	
	public ArrayList<BoardSet> selectAll(BoardVO bVO,int count){
		conn=JDBCUtil.connect();
		ArrayList<BoardSet> datas=new ArrayList<BoardSet>();

		try {
			pstmt=conn.prepareStatement(SQL_SELECTALL);
			pstmt.setInt(1, count);
			rs=pstmt.executeQuery();

			while(rs.next()) {
				BoardSet bs=new BoardSet();
				
				BoardVO data=new BoardVO();
				data.setBid(rs.getInt("BID"));
				data.setBcontent(rs.getString("BCONTENT"));
				data.setMid(rs.getString("MID"));
				data.setFavCnt(rs.getInt("FAVCNT"));
				
				bs.setBoard(data);
				
				//////////
				pstmt=conn.prepareStatement(SQL_SELECTALL_REPLY);
				pstmt.setInt(1, data.getBid());
				ResultSet rs2=pstmt.executeQuery();
				ArrayList<ReplyVO> rdatas=new ArrayList<ReplyVO>();
				while(rs2.next()) {
					ReplyVO rVO=new ReplyVO();
					rVO.setBid(rs2.getInt("BID"));
					rVO.setDate(rs2.getDate("DATE"));
					rVO.setMid(rs2.getString("MID"));
					rVO.setRid(rs2.getInt("RID"));
					rVO.setRcontent(rs2.getString("RCONTENT"));
					rdatas.add(rVO);
				}
				//////////
				
				bs.setRdatas(rdatas);
				
				datas.add(bs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		JDBCUtil.disconnect(rs, pstmt, conn);

		return datas;
	}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="kim" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>메인 페이지</title>
</head>
<body>

<script type="text/javascript">
	function signup(){
		window.open("signup.jsp","회원가입 창","width=450,height=300");
	}
</script>

<div id="header">
	<h1>SNS 커뮤니티 실습 프로젝트</h1>
</div>

<div id="gnb">
	<ul>
		<li><a href="main.do">메인으로 가기</a></li>
		<kim:login />
	</ul>
</div>

<div id="content">
	<section>
		<kim:write type="board" />
	</section>
	<hr>
	<section>
		<c:forEach var="v" items="${datas}">
			<c:set var="board" value="${v.board}" />
			<c:set var="rdatas" value="${v.rdatas}" />
			${board.bid} ${board.bcontent} | <a href="fav.do?bid=${board.bid}">♥</a>[${board.favCnt}] <br>
			<c:forEach var="r" items="${rdatas}">
				${r.rid} ${r.rcontent} <br>
			</c:forEach>
			<kim:write type="reply" bid="${board.bid}" />
			<hr>
		</c:forEach>
		
		<a href="main.do?count=${count+2}">[더보기]</a>
		<!-- 페이지네이션 -->
	</section>
</div>

<div id="footer">
	<h3>&copy; NAVER corp. | 회사소개 | 이용약관 | 개인정보처리방침 | 고객센터</h3>
</div>

</body>
</html>

https://github.com/jihyean/Web/tree/main/day49

 

728x90
반응형
Comments