- 클래스 하나 만들어서, 메소드 하나 생성
package com.bs.spring.common;
public class PageFactory {
public static String getPage(int cPage, int numPerpage, int totalData, String url) {
StringBuffer pageBar= new StringBuffer();
int totalPage=(int)(Math.ceil((double)totalData/numPerpage));
int pageBarSize=5;
int pageNo = ((cPage-1)/pageBarSize)*pageBarSize+1;
int pageEnd = pageNo+pageBarSize-1;
pageBar.append("<ul class='pagination justify-content-center pagination-sm'>");
if(pageNo==1) {
pageBar.append("<li class='page-item disabled'>");
pageBar.append("<a class='page-link' href='#'>이전");
pageBar.append("</a>");
pageBar.append("</li>");
}else {
pageBar.append("<li class='page-item'>");
pageBar.append("<a class='page-link' href='javascript:fn_paging("+(pageNo-1)+")'>이전");
pageBar.append("</a>");
pageBar.append("</li>");
}
while(!(pageNo>pageEnd || pageNo>totalPage)) {
if(pageNo==cPage) {
pageBar.append("<li class='page-item disabled'>");
pageBar.append("<a class='page-link' href='#'>"+pageNo);
pageBar.append("</a>");
pageBar.append("</li>");
}else {
pageBar.append("<li class='page-item'>");
pageBar.append("<a class='page-link' href='javascript:fn_paging("+(pageNo)+")'>"+pageNo);
pageBar.append("</a>");
pageBar.append("</li>");
}
pageNo++;
}
if(pageNo>totalPage) {
pageBar.append("<li class='page-item disabled'>");
pageBar.append("<a class='page-link' href='#'>다음");
pageBar.append("</a>");
pageBar.append("</li>");
}else {
pageBar.append("<li class='page-item'>");
pageBar.append("<a class='page-link' href='javascript:fn_paging("+(pageNo)+")'>다음");
pageBar.append("</a>");
pageBar.append("</li>");
}
pageBar.append("</ul>");
pageBar.append("<script>");
pageBar.append("function fn_paging(no){");
pageBar.append("location.assign('"+url+"?cPage='+no+'&numPerpage="+numPerpage+"');");
pageBar.append("}");
pageBar.append("</script>");
return new String(pageBar);
}
}
컨트롤러
에서 페이징 처리 메소드 불러오기
@RequestMapping("/board/selectBoardAll.do")
public String boardList(@RequestParam(value="cPage",defaultValue = "1") int cPage
, @RequestParam(value="numPerpage",defaultValue = "5") int numPerPage, Model m) {
List<Board> boards = service.selectBoardList(Map.of("cPage",cPage,"numPerpage",numPerPage));
int totalData = service.selectBoardCount();
m.addAttribute("pageBar",PageFactory.getPage(cPage, numPerPage, totalData,"selectBoardAll.do"));
m.addAttribute("boards",boards);
m.addAttribute("totalData",totalData);
return "board/boardList";
}
서비스
처리
@Override
public List<Board> selectBoardList(Map<String, Object> param) {
return dao.selectBoardList(session,param);
}
@Override
public int selectBoardCount() {
return dao.selectBoardCount(session);
}
dao
처리
@Override
public List<Board> selectBoardList(SqlSession session, Map<String, Object> param) {
int cPage=(int)param.get("cPage");
int numPerpage=(int)param.get("numPerpage");
RowBounds rb = new RowBounds((cPage-1)*numPerpage,numPerpage);
return session.selectList("board.selectBoardList",null,rb);
}
@Override
public int selectBoardCount(SqlSession session) {
return session.selectOne("board.selectBoardCount");
}
- mapper
<mapper namespace="board">
<select id="selectBoardList" resultType="board">
SELECT * FROM BOARD
</select>
<select id="selectBoardCount" resultType="_int">
SELECT COUNT(*) FROM BOARD
</select>
</mapper>
- jsp 화면
<section id="board-container" class="container">
<p>총 ${totalData }건의 게시물이 있습니다.</p>
<table id="tbl-board" class="table table-striped table-hover">
<tr>
<th>번호</th>
<th>제목</th>
<th>작성자</th>
<th>작성일</th>
<th>첨부파일</th>
<th>조회수</th>
</tr>
<c:if test="${not empty boards}">
<c:forEach var="b" items="${boards }">
<tr>
<td>${b.boardNo }</td>
<td><a href="${path}/board/detailBoard.do?boardNo=${b.boardNo}">${b.boardTitle}</a></td>
<td>${b.boardWriter }</td>
<td>${b.boardDate }</td>
<td></td>
<td>${b.boardReadCount }</td>
</tr>
</c:forEach>
</c:if>
</table>
<div id="pageBar">
<c:out value="${pageBar }" escapeXml="false"/>
</div>
</section>
정말 좋은 글 감사합니다!