페이징 처리 대응
This commit is contained in:
@@ -8,6 +8,7 @@ import java.util.Map.Entry;
|
|||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import kr.itpinc.first.board.service.BoardService;
|
import kr.itpinc.first.board.service.BoardService;
|
||||||
import kr.itpinc.first.common.common.CommandMap;
|
import kr.itpinc.first.common.common.CommandMap;
|
||||||
|
import kr.itpinc.first.common.util.CommonUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
@@ -31,7 +32,7 @@ public class BoardController
|
|||||||
@Autowired
|
@Autowired
|
||||||
private BoardService boardService; //샘플서비스 선언하고
|
private BoardService boardService; //샘플서비스 선언하고
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@RequestMapping(value="/sample/openBoardSearch.do")
|
@RequestMapping(value="/sample/openBoardSearch.do")
|
||||||
public ModelAndView openBoardSearch(CommandMap commandMap) throws Exception
|
public ModelAndView openBoardSearch(CommandMap commandMap) throws Exception
|
||||||
@@ -52,6 +53,7 @@ public class BoardController
|
|||||||
// 이 안에서 pageable 객체를 열어보면 계산이 다 끝나 있습니다.
|
// 이 안에서 pageable 객체를 열어보면 계산이 다 끝나 있습니다.
|
||||||
int currentPage = pageable.getPageNumber(); // 현재 페이지 번호 (0부터 시작)
|
int currentPage = pageable.getPageNumber(); // 현재 페이지 번호 (0부터 시작)
|
||||||
int pageSize = pageable.getPageSize(); // 페이지당 데이터 수 (10)
|
int pageSize = pageable.getPageSize(); // 페이지당 데이터 수 (10)
|
||||||
|
long totalCount = 0;
|
||||||
long offset = pageable.getOffset(); // DB 쿼리용 시작 위치 (0 * 10 = 0)
|
long offset = pageable.getOffset(); // DB 쿼리용 시작 위치 (0 * 10 = 0)
|
||||||
Map<String, Object> param = commandMap.getMap();
|
Map<String, Object> param = commandMap.getMap();
|
||||||
param.put("PAGE_INDEX", currentPage);
|
param.put("PAGE_INDEX", currentPage);
|
||||||
@@ -59,14 +61,15 @@ public class BoardController
|
|||||||
param.put("START", offset);
|
param.put("START", offset);
|
||||||
|
|
||||||
|
|
||||||
List<Map<String, Object>> resultMap = boardService.selectBoardList(param);
|
List<Map<String, Object>> resultList = boardService.selectBoardList(param);
|
||||||
|
|
||||||
|
|
||||||
// 🌟 원인 파악을 위한 디버깅 코드 3줄 추가 🌟
|
// 🌟 원인 파악을 위한 디버깅 코드 3줄 추가 🌟
|
||||||
System.out.println("넘어온 파라미터: " + commandMap.getMap());
|
log.debug("넘어온 파라미터: " + commandMap.getMap());
|
||||||
System.out.println("DB 조회 건수: " + resultMap.size());
|
log.debug("DB 조회 건수: " + resultList.size());
|
||||||
if(!resultMap.isEmpty()) {
|
if(!resultList.isEmpty()) {
|
||||||
System.out.println("첫 번째 데이터 형태: " + resultMap.get(0));
|
log.debug("첫 번째 데이터 형태: " + resultList.get(0));
|
||||||
|
totalCount = Long.parseLong(String.valueOf(resultList.get(0).get("TOTAL_COUNT")) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -74,11 +77,14 @@ public class BoardController
|
|||||||
//앞에서 샘플서비스 선언한것 뒤에 selectBoardList라는 메서드 만들고
|
//앞에서 샘플서비스 선언한것 뒤에 selectBoardList라는 메서드 만들고
|
||||||
//mv.addObject("list", list); //앞에서 실행 다하고 list에 담긴건 list라고 선언하고
|
//mv.addObject("list", list); //앞에서 실행 다하고 list에 담긴건 list라고 선언하고
|
||||||
|
|
||||||
//mv.addObject("paginationInfo", (PaginationInfo)resultMap.get("paginationInfo"));
|
|
||||||
log.info(resultMap.toString());
|
|
||||||
mv.addObject("list", resultMap);
|
|
||||||
|
|
||||||
log.info("");
|
//페이징렌더링용 Map데이터 출력
|
||||||
|
Map<String, Object> pagingMap = CommonUtils.makePagingMap(currentPage, totalCount, pageSize, 10);
|
||||||
|
mv.addObject("list", resultList);
|
||||||
|
mv.addObject("pagingInfo", pagingMap);
|
||||||
|
log.info(resultList.toString());
|
||||||
|
log.info(pagingMap.toString());
|
||||||
|
|
||||||
return mv;
|
return mv;
|
||||||
|
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ public class AbstractDAO {
|
|||||||
if(pageIndex < 1) {
|
if(pageIndex < 1) {
|
||||||
pageIndex = 1;
|
pageIndex = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 정상적인 START, END 계산
|
// 2. 정상적인 START, END 계산
|
||||||
int end = pageIndex * pageRow;
|
int end = pageIndex * pageRow;
|
||||||
int start = (pageIndex - 1) * pageRow + 1;
|
int start = (pageIndex - 1) * pageRow + 1;
|
||||||
@@ -142,6 +142,7 @@ public class AbstractDAO {
|
|||||||
|
|
||||||
return sqlSession.selectList(queryId, map);
|
return sqlSession.selectList(queryId, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package kr.itpinc.first.common.util;
|
package kr.itpinc.first.common.util;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommonUtils
|
public class CommonUtils
|
||||||
{
|
{
|
||||||
@@ -8,4 +10,25 @@ public class CommonUtils
|
|||||||
{
|
{
|
||||||
return UUID.randomUUID().toString().replaceAll("-", "");
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<String, Object> makePagingMap(int currentPage, long totalCount, int pageSize, int pageBlockSize) {
|
||||||
|
Map<String, Object> paging = new HashMap<>();
|
||||||
|
|
||||||
|
// 전체 페이지 수 계산
|
||||||
|
int totalPages = (int) Math.ceil((double) totalCount / pageSize);
|
||||||
|
if (totalPages == 0) totalPages = 1;
|
||||||
|
|
||||||
|
// 페이지 블록 시작/끝 번호 계산 (예: 1~10, 11~20)
|
||||||
|
int startPage = ((currentPage - 1) / pageBlockSize) * pageBlockSize + 1;
|
||||||
|
int endPage = Math.min(startPage + pageBlockSize - 1, totalPages);
|
||||||
|
|
||||||
|
paging.put("currentPage", currentPage);
|
||||||
|
paging.put("totalPages", totalPages);
|
||||||
|
paging.put("startPage", startPage);
|
||||||
|
paging.put("endPage", endPage);
|
||||||
|
paging.put("hasPrev", startPage > 1);
|
||||||
|
paging.put("hasNext", endPage < totalPages);
|
||||||
|
|
||||||
|
return paging;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
||||||
|
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
|
||||||
|
<div class="pagination">
|
||||||
|
<c:if test="${pagingInfo.hasPrev}">
|
||||||
|
<a href="javascript:fn_search(${pagingInfo.startPage - 1});">[이전]</a>
|
||||||
|
</c:if>
|
||||||
|
<c:forEach var="p" begin="${pagingInfo.startPage}" end="${pagingInfo.endPage}">
|
||||||
|
<c:choose>
|
||||||
|
<c:when test="${p == pagingInfo.currentPage}">
|
||||||
|
<strong>[${p}]</strong>
|
||||||
|
</c:when>
|
||||||
|
<c:otherwise>
|
||||||
|
<a href="javascript:fn_search(${p});">[${p}]</a>
|
||||||
|
</c:otherwise>
|
||||||
|
</c:choose>
|
||||||
|
</c:forEach>
|
||||||
|
<c:if test="${pagingInfo.hasNext}">
|
||||||
|
<a href="javascript:fn_search(${pagingInfo.endPage + 1});">[다음]</a>
|
||||||
|
</c:if>
|
||||||
|
</div>
|
||||||
@@ -49,8 +49,8 @@
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<c:if test="${not empty paginationInfo }">
|
<c:if test="${not empty pagingInfo }">
|
||||||
<ui:pagination paginationInfo = "${paginationInfo }" type="text" jsFunction="fn_search"/>
|
<%@ include file="/WEB-INF/include/include-page.jspf" %>
|
||||||
</c:if>
|
</c:if>
|
||||||
<input type="hidden" id="currentPageNo" name="currentPageNo"/>
|
<input type="hidden" id="currentPageNo" name="currentPageNo"/>
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@
|
|||||||
{
|
{
|
||||||
var comSubmit = new ComSubmit();
|
var comSubmit = new ComSubmit();
|
||||||
comSubmit.setUrl("<c:url value='/sample/openBoardList.do'/>");
|
comSubmit.setUrl("<c:url value='/sample/openBoardList.do'/>");
|
||||||
comSubmit.addParam("currentPageNo", pageNo);
|
comSubmit.addParam("page", pageNo);
|
||||||
comSubmit.submit();
|
comSubmit.submit();
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user