[Spring] 12. 게시판 - 리스트 페이지네이션개발자가 되기까지 (2023.08.16~2024.04.15)/[Spring] Basic Web2024. 1. 17. 13:17
Table of Contents
페이지네이션을 진행할 만큼 게시물의 수가 넉넉하지 않다면
https://rlog0918.tistory.com/350을 참고해 단위 테스트를 진행하여 게시물을 넉넉하게 생성한다.
PagingVO.java
package com.basicWeb.www.domain;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
@Getter
public class PagingVO {
private int pageNo;
private int qty;
public PagingVO() {
this.pageNo = 1;
this.qty = 10;
}
public PagingVO(int pageNo, int qty) {
this.pageNo = pageNo;
this.qty = qty;
}
public int getStartPage() {
return (this.pageNo-1) * 10;
}
}
src / main / java 하위에 handler package를 생성하고 PagingHandler class를 작성
PagingHandler.java
package com.basicWeb.www.handler;
import com.basicWeb.www.domain.PagingVO;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
@Getter
public class PagingHandler {
private int startPage;
private int endPage;
private int totalCount;
private PagingVO pgvo;
private Boolean prev, next;
public PagingHandler(PagingVO pgvo, int totalCount) {
this.pgvo = pgvo;
this.totalCount = totalCount;
this.endPage = (int) Math.ceil(pgvo.getPageNo() / (double) pgvo.getQty()) * pgvo.getQty();
this.startPage = endPage - 9;
int realEndPage = (int)(Math.ceil(totalCount / (double) pgvo.getQty()));
if(realEndPage < endPage) {
this.endPage = realEndPage;
}
this.prev = startPage > 1;
this.next = this.endPage < realEndPage;
}
}
BoardController.java
package com.basicWeb.www.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.basicWeb.www.domain.BoardVO;
import com.basicWeb.www.domain.PagingVO;
import com.basicWeb.www.handler.PagingHandler;
import com.basicWeb.www.service.BoardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequestMapping("/board/*")
@RequiredArgsConstructor
@Controller
public class BoardController {
private final BoardService bsv;
// ... (기존 코드)
@GetMapping("/list")
public void list (Model m, PagingVO pgvo) {
log.info(">>> pgvo >>> {}", pgvo);
List<BoardVO> list = bsv.getList(pgvo);
int totalCount = bsv.totalCount();
PagingHandler ph = new PagingHandler(pgvo, totalCount);
m.addAttribute("list", list);
m.addAttribute("ph", ph);
}
// ... (기존 코드)
}
getList() Method에 매개변수 pgvo를 주어 페이지네이션을 해서 데이터를 뿌릴 수 있도록 한다.
ph는 전체적인 게시물 수를 가지고 페이징 라인을 만들기 위해 작성했다.
BoardService.interface
package com.basicWeb.www.service;
import java.util.List;
import com.basicWeb.www.domain.BoardVO;
import com.basicWeb.www.domain.PagingVO;
public interface BoardService {
void register(BoardVO bvo);
List<BoardVO> getList(PagingVO pgvo);
BoardVO getDetail(long bno);
void modify(BoardVO bvo);
void remove(BoardVO bvo);
int totalCount();
}
BoardServiceImpl.java
package com.basicWeb.www.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.basicWeb.www.domain.BoardVO;
import com.basicWeb.www.domain.PagingVO;
import com.basicWeb.www.repository.BoardDAO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequiredArgsConstructor
@Service
public class BoardServiceImpl implements BoardService{
private final BoardDAO bdao;
// ... (기존 코드)
@Override
public List<BoardVO> getList(PagingVO pgvo) {
return bdao.getList(pgvo);
}
// ... (기존 코드)
@Override
public int totalCount() {
// TODO Auto-generated method stub
return bdao.totalCount();
}
}
BoardDAO.interface
package com.basicWeb.www.repository;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.basicWeb.www.domain.BoardVO;
import com.basicWeb.www.domain.PagingVO;
public interface BoardDAO {
void register(BoardVO bvo);
List<BoardVO> getList(PagingVO pgvo);
BoardVO getDetail(long bno);
void update(BoardVO bvo);
void delete(BoardVO bvo);
void upReadCount(@Param("bno") long bno, @Param("count") int count);
int totalCount();
}
boardMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.basicWeb.www.repository.BoardDAO">
<!-- ... (기존 코드) -->
<select id="getList" resultType="com.basicWeb.www.domain.BoardVO">
SELECT * FROM board
ORDER BY bno desc
LIMIT #{startPage}, #{qty}
</select>
<!-- ... (기존 코드) -->
<select id="totalCount" resultType="int">
SELECT COUNT(bno) FROM board
</select>
</mapper>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:include page="../layout/header.jsp"></jsp:include>
<jsp:include page="../layout/nav.jsp"></jsp:include>
<div class="container-md">
<table class="table">
<!-- ... (기존 코드) -->
</table>
<!-- 페이징 라인 -->
<nav aria-label="Page navigation example">
<ul class="pagination">
<li class="page-item ${(ph.prev eq false) ? 'disabled' : '' }">
<a class="page-link"
href="/board/list?pageNo=${ph.startPage-1 }&qty=${ph.pgvo.qty}"
aria-label="Previous"><span aria-hidden="true">«</span></a>
</li>
<c:forEach begin="${ph.startPage }" end="${ph.endPage }" var="i">
<li class="page-item">
<a class="page-link"
href="/board/list?pageNo=${i }&qty=${ph.pgvo.qty}">${i}</a>
</li>
</c:forEach>
<li class="page-item ${(ph.next eq false) ? 'disabled' : '' }">
<a class="page-link" href="/board/list?pageNo=${ph.endPage+1 }&qty=${ph.pgvo.qty}" aria-label="Next"><span aria-hidden="true">»</span></a>
</li>
</ul>
</nav>
</div>
<jsp:include page="../layout/footer.jsp"></jsp:include>
<페이지 네이션>
[Spring] 12. 게시판 - 리스트 페이지네이션
(다음 게시물 예고편)
[Spring] 13. 게시판 - 검색
728x90
@rlozlr :: 얼렁뚱땅 개발자
얼렁뚱땅 주니어 개발자
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!