모델2(MVC) 게시판 만들기 - 글목록 보기


- Board.class와 BoardDao.class는 모델1방식(http://injava.tistory.com/20참고)과 동일하다.(Model 역활)

- BoardListServlet은 BoardDao의 메서드를 호출하여 필요한 값(model이라고도 부름)들을 request의 속성에 저장하고 boardList.jsp로 포워딩한다.

- boardList.jsp는 자바관련코드를 사용하지 않도록 EL/JSTL을 사용한다.



BoardListServlet.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
package web;
 
import java.io.IOException;
import java.util.List;
 
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 service.Board;
import service.BoardDao;
 
@WebServlet("/board/boardList")
public class BoardListServlet extends HttpServlet {
    private BoardDao boardDao;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int currentPage = 1;
        if(request.getParameter("currentPage"!= null) {
            currentPage = Integer.parseInt(request.getParameter("currentPage"));
        }
        
        boardDao = new BoardDao();
        int totalRowCount = boardDao.selectTotalBoardCount();
        int pagePerRow = 10
        int beginRow = (currentPage-1)*pagePerRow;
        int lastPage = totalRowCount/pagePerRow;
        if(totalRowCount%pagePerRow != 0) {
            lastPage++;
        }
        List<Board> list = boardDao.selectBoardListPerPage(beginRow, pagePerRow);
        request.setAttribute("currentPage", currentPage);
        request.setAttribute("totalRowCount", totalRowCount);
        request.setAttribute("pagePerRow", pagePerRow);
        request.setAttribute("lastPage", lastPage);
        request.setAttribute("list", list);
        request.getRequestDispatcher("/board/boardList.jsp").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
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title></title>
</head>
<body>
    <div>전체행의 수 : ${totalRowCount}/${list.size()}</div>
    <table border="1">
        <thead>
            <tr>
                <th>boardTitle</th>
                <th>boardUser</th>
                <th>boardDate</th>
            </tr>
        </thead>
        <tbody>
            <c:forEach var="b" items="${list}">
                <tr>
                    <td><a href="<%=request.getContextPath()%>/board/boardView?boardNo=${b.boardNo}">${b.boardTitle}</a></td>
                    <td>${b.boardUser}</td>
                    <td>${b.boardDate}</td>
                </tr>
            </c:forEach>
        </tbody>
    </table>
 
    <div>
        <a href="<%=request.getContextPath()%>/board/boardAdd">게시글 입력</a>
    </div>
    <div>
        <c:if test="${currentPage>1}">
            <a href="<c:url value='/board/boardList?currentPage=${currentPage-1}'/>">이전</a>
        </c:if>
        <c:if test="${currentPage < lastPage}"
            <a href="<c:url value='/board/boardList?currentPage=${currentPage+1}'/>">다음</a>
        </c:if>
 
    </div>
</body>
</html>
cs



+ Recent posts