2016년 10월 7일 금요일

53day / WEB / Model2 게시판 (Paging)


51Day게시판 + Subquery적용하여 게시판 Paging하기!




  • 구동화면
미리 정해놓은 갯수를 초과하면
다음 페이지로 이동하는 a태그 화살표를 생성한다.

다음 페이지로 이동했을 때는 이전 페이지 화살표를 만들어준다.


  • 추가된 부분
 * 비즈니스 계층의 주요 구성요소 업무 정의(Transaction : 작업단위)
    컨트롤러 영역에서 모델을 접근하는 인터페이스(개념적)역할을 한다
        -> Session facade Pattern

public class BoardService {
    private static BoardService instance = new BoardService();
    private BoardDAO boardDAO;
    private BoardService() {
        boardDAO = BoardDAO.getInstance();
    }
    public static BoardService getInstance() {
        return instance;
    }
     * 게시글 등록

    public void posting(BoardVO vo) throws SQLException {
        boardDAO.posting(vo);
    }
  
     * 상세 게시글 조회 조회시 조회수를 업데이트한다
    public BoardVO getPostingByNo(int no) throws SQLException {
        boardDAO.updateHit(no);
        return boardDAO.getPostingByNo(no);
    }
     * 상세 게시글 조회하되 조회수 증가시키지 않는다 글등록과 수정시 사용한다
    public BoardVO getPostingByNoNotHit(int no) throws SQLException {
        return boardDAO.getPostingByNo(no);
    }
    
     * 수정, 삭제시 비밀번호 일치여부를 판단
    public boolean checkPassword(int no, String password) throws SQLException {
        return boardDAO.checkPassword(no, password);
    }
     * 게시글 삭제

    public void deletePosting(int no) throws SQLException {
        boardDAO.deletePosting(no);
    }
    
     * 게시글 수정

    public void updatePosting(BoardVO vo) throws SQLException {
        boardDAO.updatePosting(vo);
    }
  
     * 게시물 리스트를 반환
    
    public ListVO getPostingList(String pageNo) throws SQLException {
        int totalCount = boardDAO.getTotalContentCount();
        PagingBean pagingBean = null;
        if(pageNo == null){
            pagingBean = new PagingBean(totalCount);
        }else{
            pagingBean = new PagingBean(totalCount, Integer.parseInt(pageNo));
        }
        return new ListVO(boardDAO.getPostingList(pagingBean), pagingBean);
    }
}
cs

 *     페이징 처리를 위한 비즈니스 클래스
public class PagingBean {
     * 현재 페이지
    private int nowPage = 1;

     * 페이지 당 보여줄 게시물 수
    private int contentNumberPerPage = 5;

     * 페이지 그룹 당 페이지 수
    private int pageNumberPerPageGroup = 4;

     * 현재 db에 저장된 총 게시물 수
    private int totalContents;

    public PagingBean(){}

     * 총게시물수를 할당
    public PagingBean(int totalContents) {
        this.totalContents = totalContents;
    }

     * 총게시물수와 현재 페이지 정보를 할당
    public PagingBean(int totalContents, int nowPage) {
        this.totalContents = totalContents;
        this.nowPage = nowPage;
    }

     * 현재 페이지 리턴
    public int getNowPage() {
        return nowPage;
    }

     * 현 페이지에서 보여줄 행(row) 의 시작번호
    public int getStartRowNumber() {
        return ((nowPage - 1* contentNumberPerPage) + 1;
    }

     * 현 페이지에서 보여줄 행(row)의 마지막 번호
    public int getEndRowNumber() {
        int endRowNumber = nowPage * contentNumberPerPage;
        if (totalContents < endRowNumber)
            endRowNumber = totalContents;
        return endRowNumber;
    }

     * 총 페이지 수를 return한다.
    public int getTotalPage() {
        int totalPage = 0;
        if(totalContents % contentNumberPerPage == 0){
            totalPage = totalContents / contentNumberPerPage;
        }
        if(totalContents % contentNumberPerPage > 0){
            totalPage = (totalContents / contentNumberPerPage) + 1;
        }
        return totalPage;
    }

     * 총 페이지 그룹의 수를 return한다.
    public int getTotalPageGroup() {
        int totalPageGroup = 0;
        if(getTotalPage() % pageNumberPerPageGroup == 0){
            totalPageGroup = getTotalPage() / pageNumberPerPageGroup;
        }
        if(getTotalPage() % pageNumberPerPageGroup > 0){
            totalPageGroup = (getTotalPage() / pageNumberPerPageGroup) + 1;
        }
        return totalPageGroup;
    }

     * 현재 페이지가 속한 페이지 그룹 번호(몇 번째 페이지 그룹인지) 을 return 하는 메소드
    public int getNowPageGroup() {
        int nowPageGroup = 0;
        if(getNowPage() % pageNumberPerPageGroup == 0){
            nowPageGroup = getNowPage() / pageNumberPerPageGroup;
        }
        if(getNowPage() % pageNumberPerPageGroup > 0){
            nowPageGroup = (getNowPage() / pageNumberPerPageGroup) + 1 ;
        }
        return nowPageGroup;
    }

     * 현재 페이지가 속한 페이지 그룹의 시작 페이지 번호를 return 한다.
    public int getStartPageOfPageGroup() {
        int startPageOfPageGroup;
        startPageOfPageGroup = (pageNumberPerPageGroup * (getNowPageGroup() -1)) + 1;
        return startPageOfPageGroup;
    }

     * 현재 페이지가 속한 페이지 그룹의 마지막 페이지 번호를 return 한다.
    public int getEndPageOfPageGroup() {
        int endPageOfPageGroup = pageNumberPerPageGroup * getNowPageGroup();
        if(endPageOfPageGroup > getTotalPage()){
            endPageOfPageGroup = getTotalPage();
        }
        return endPageOfPageGroup;
    }

     * 이전 페이지 그룹이 있는지 체크하는 메서드
    public boolean isPreviousPageGroup() {
        boolean flag = false;
        if(getNowPageGroup() > 1){
            flag = true;
        }
        return flag;
    }
     * 다음 페이지 그룹이 있는지 체크하는 메서드
    public boolean isNextPageGroup() {
        boolean flag = false;
        if(getNowPageGroup() < getTotalPageGroup()){
            flag = true;
        }
        return flag;
    }
}
cs


public class ListController implements Controller {
    @Override
    public ModelAndView execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        * 게시판 번호(a 태그에서 전달받는다.)
        String pageNo = request.getParameter("pageNo");
        ListVO list = BoardService.getInstance().getPostingList(pageNo);
        return new ModelAndView("board/list.jsp""vo", list);
    }
}
cs


<%@page import="model.PagingBean"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>글목록</title>
<link rel="stylesheet" href="${initParam.root}css/board.css" type="text/css">
<script type="text/javascript">
</script>
</head>
<body>
    <c:import url="/template/header.jsp"></c:import>    
    <table class="list">
        <caption>목록</caption>
        <thead>
        <tr>
            <th class="no">NO</th>
            <th class="title">제목</th>
            <th class="name">이름</th>
            <th class="date">작성일</th>
            <th class="hit">HIT</th>
            </tr>
        </thead>
        <tbody>            
        <c:forEach var="board" items="${vo.list}">                
            <tr>
                <td>${board.no}</td>                
                <td><a href="${initParam.root}DispatcherServlet?command=showContent&no=${board.no }">
                ${board.title}</a></td>
                <td>${board.writer}</td>
                <td>${board.timePosted}</td>
                <td>${board.hits}</td>
            </tr>
            </c:forEach>
        </tbody>    
    </table>
    <a href="${initParam.root}board/write.jsp"><img src="${initParam.root}img/write_btn.jpg" border="0"></a>
        <p class="paging">

      *  이전 버튼
        <c:if test="${vo.pagingBean.previousPageGroup == true}">
            <a href="DispatcherServlet?command=list&pageNo=${vo.pagingBean.startPageOfPageGroup -1 }"> < </a>
        </c:if>

 *  반복문을 이용하여 페이지를 뽑늗다
    <c:forEach var="page" begin = "${vo.pagingBean.startPageOfPageGroup}" end = "${vo.pagingBean.endPageOfPageGroup}" varStatus="order">
        <a href="DispatcherServlet?command=list&pageNo=${order.index}">${order.index}</a>
    </c:forEach>

      *  다음 버튼
        <c:if test="${vo.pagingBean.nextPageGroup == true}">
        <a href="DispatcherServlet?command=list&pageNo=${vo.pagingBean.endPageOfPageGroup +1 }"> > </a>
        </c:if>


</body>
</html>
cs

0 개의 댓글:

댓글 쓰기