본문 바로가기
JSP/JSP & Servlet

MVC패턴 게시판 만들기 - 전체 게시글 보기

by avvin 2019. 5. 24.
MVC패턴 게시판 만들기 - 전체 게시글 보기



BoardListCon.java (서블릿 클래스)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package control;
 
import java.io.IOException;
import java.util.Vector;
 
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import model.BoardBean;
import model.BoardDAO;
 
@WebServlet("/BoardListCon.do")
public class BoardListCon extends HttpServlet {
    
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        reqPro(request, response);
    }
 
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        reqPro(request, response);
    }
 
    
    protected void reqPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //jsp에 있던 자바코드들을 그대로 가져와서 request에 붙여주면된다.
        //10개씩 끊어서 가지고오기
        //화면에 보여질 게시글의 개수를 지정
        int pageSize = 10;
        //현재 카운터를 클릭한 번호값을 읽어옴
        //  이전   1 2 3 4 5  다음
        String pageNum = request.getParameter("pageNum");
        //만약 처음 BoardList.jsp를 클릭하거나 수정, 삭제 등 
        //다른 게시글에서 이 페이지로 넘어오는 pageNum값이 없다면 null처리
        //=다른 페이지에서 넘어왔기 때문에 가장 최신 글목록을 보여준다.
        if (pageNum==null){
            pageNum="1";
        }
        int count = 0//전체 글의 갯수를 저장하는 변수
        int number =0// 페이지 넘버링 변수
 
        //현재 보고자하는 페이지 숫자를 저장
        int currentPage = Integer.parseInt(pageNum);
 
 
        //전체 게시글의 숫자, 제목을 jsp쪽으로 가져와야함
        BoardDAO bdao = new BoardDAO();
        
        //전체 게시글의 갯수를 읽어들인 메서드 호출
        count = bdao.getAllCount();
        //현재 페이지에 보여줄 시작 번호를 설정 = DB에서 불러올 시작번호
        //★★★★★
        int startRow =(currentPage-1)*pageSize+1;
        int endRow = (currentPage)*pageSize;
        
        //최신 글 10개를 기준으로 게시글을 리턴 받아주는 메서드 호출
        //(bean이라는 세트 여러개를 한꺼번에 받아오려면 리스트(벡터) 써야한다.)
        Vector<BoardBean> vec = bdao.getAllBoard(startRow, endRow);
        //테이블에 표시할 번호 지정
        number = count - (currentPage - 1 ) * pageSize;
        
    
        /////BoardList.jsp쪽으로 request객체에 담아서 넘겨줌
        
        request.setAttribute("vec", vec);
        request.setAttribute("number", number);
        request.setAttribute("pageSize", pageSize);
        request.setAttribute("count", count);
        request.setAttribute("currentPage", currentPage);
        
        RequestDispatcher rd = request.getRequestDispatcher("BoardList.jsp");
        rd.forward(request, response);
        
        
    }
    
}
 
cs



BoardList.jsp


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri ="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<body>
 
<div style="text-align: center">
<h2>전체 게시글 보기</h2>
<!-- 게시글 보기에 카운터링을 설정하기 위한 변수들을 선언 -->
 
 
<table align="center" width="700" border="1" bgcolor=skyblue>
    <tr height="40">
        <td align="right" colspan="5"><input type="button" value="글쓰기"
            onclick="location.href='BoardWriteForm.jsp'"></td>
    </tr>
    <tr height="40">
        <td width="50" align="center">번호</td>
        <td width="320" align="center">제목</td>
        <td width="100" align="center">작성자</td>
        <td width="150" align="center">작성일</td>
        <td width="80" align="center">조회수</td>
    </tr>
 
    <!-- 벡터에 저장돼있는 빈클래스를 하나씩 추출 
    for문 대신 foreach 태그 사용-->
    
    <!-- 넘어온 number값을 1씩 감소시키며 출력해야한다. 
    스크립트릿으로는 number--하면 되지만 EL에선 안됨-->
    <c:set var="number" value="${number }"/>
    
    <!-- 한페이지에 보여줄 게시글 수 만큼만 vec에 담겨서 넘어옴 -->
    <c:forEach var = "bean" items= "${vec }">
    
        <tr height="40">
            <td width="50" align="center">${number }</td>
            <td width="320" align="left">
            
            <!-- re_step이 1보다 크면(최상위 글이 아니면)단계만큼 들여쓰기 -->
            <c:if test="${bean.re_step > 1 }">
                <c:forEach var = "j" begin="1" end ="${(bean.re_step-1)*5 }">
                &nbsp;
                </c:forEach>
            </c:if>
            <a href="BoardInfoControl.do?num=${bean.num }"> ${bean.subject } </a>
            </td>
            <td width="100" align="left">${bean.writer }</td>
            <td width="150" align="left">${bean.reg_date }</td>
            <td width="80" align="left">${bean.readcount }</td>
        </tr>
        
    <c:set var="number" value="${number-1 }"/>
    </c:forEach>
 
</table>
 
<p>
 
<!-- 카운터링 -->
<c:if test = "${count>0}">
    <c:set var="pageCount" value="${count /pageSize + (count%pageSize==0 ? 0:1)}"/>
    
    <c:set var="startPage" value="1"/>        
    
    <c:if test="${currentPage%10 != 0 }">        
        <!-- 결과를 정수형으로 받기★★★ -->    
        <fmt:parseNumber var="result" value="${currentPage/10 }" integerOnly="true"/>    
        <c:set var="startPage" value="${result*10+1 }"/>
    </c:if>
    <c:if test="${currentPage%10 == 0 }">        
        <c:set var="startPage" value="${(result-1)*10+1 }"/>
    </c:if>
    
    
    <!-- 화면에 보여질 페이지 처리 숫자를 표현 -->
    
    <c:set var ="pageBlock" value="${10 }"/>
    <c:set var ="endPage" value="${startPage + pageBlock -1}"/>    
    
    <c:if test="${endPage > pageCount }">
        <c:set var="endPage" value="${pageCount}"/>
    </c:if>
    
    
    <!-- [이전]링크 걸지 파악 -->    
    <c:if test="${startPage>10}">
        <a href='BoardList.jsp?pageNum=${startPage-10}'> [이전] </a>
    </c:if>
    
    <!-- 페이징 처리 -->
    <c:forEach var="i" begin="${startPage }" end="${endPage }">
    <a href="BoardList.jsp?pageNum=${i }"> [${ i}] </a>
    </c:forEach>
    
    <!-- "다음"이라는 링크를 만들건지 파악 -->
    <c:if test="${endPage<pageCount}">
        <a href='BoardList.jsp?pageNum=${startPage-10}'> [다음] </a>
    </c:if>
    
</c:if>
 
</div>
</body>
</html>
cs