Spring

[Spring]4. 게시판 구현 - 글 목록

code-mo 2023. 2. 12. 07:30
728x90

2023.02.08 - [게시판 만들기/JSP ➜ Spring] - [Spring] 게시판 구현 페이징 처리

1. 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
<div class="row">
    <table class="table table-striped"
        style="text-align: center; border: 1px solid #dddddd">
        <thead>
            <tr>
                <th style="background-color: #eeeeee; text-align: center;">번호</th>
                <th style="background-color: #eeeeee; text-align: center;">제목</th>
                <th style="background-color: #eeeeee; text-align: center;">작성자</th>
                <th style="background-color: #eeeeee; text-align: center;">작성일</th>
            </tr>
            <c:forEach var="boardDTO" items="${boardList}">
                    <tr onclick="location.href='${pageContext.request.contextPath}/view?bbsID=${boardDTO.bbsID}'">
                        <td>${boardDTO.bbsID}</td>
                    <td class="left">${boardDTO.bbsTitle}</td>
                    <td>${boardDTO.userID}</td>
                    <td><fmt:parseDate var="bbsDate" value="${boardDTO.bbsDate}" pattern="yyyy-MM-dd"/>
                        <fmt:formatDate value="${bbsDate}" pattern="yyyy.MM.dd"/>
                        </td>
                    </tr>     
            </c:forEach>
        </thead>
        <tbody>                
            <tr>
                <td></td>
                <td><a href="view.jsp?bbsID">
                        </a></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
    
    <c:if test="${pageVO.startPage > pageVO.pageBlock}">
        <a href="${pageContext.request.contextPath}/board?nowPage=${pageVO.startPage-pageVO.pageBlock}">Prev</a>
    </c:if>
    
    <c:forEach var="i" begin="${pageVO.startPage}" end="${ pageVO.endPage}" step="1">
        <a href="${pageContext.request.contextPath}/board?nowPage=${i}">${i}</a>
    </c:forEach>
    
    <c:if test="${endPage < pageCount }">
        <a href="${pageContext.request.contextPath}/board?nowPage=${pageVO.startPage+pageVO.pageBlock}">Next</a>
    </c:if>
    
    <c:if test="${sessionScope.userID != null}">
        <div class="pull-right">
            <a href="write" class="btn btn-primary pull-right">글쓰기</a>
        </div>
    </c:if>
</div>
cs

 

2. 게시판 페이지 Controller 

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
//페이지 - 게시판
@RequestMapping(value = "/board", method = RequestMethod.GET)
public String board(HttpServletRequest request, HttpServletResponse response,
        Model model,
        @RequestParam(defaultValue = "1"int nowPage) {
                    
    int pageSize = 10;
    int pageBlock = 10;
    
    PageVO pageVO = new PageVO();
    pageVO.setPageSize(pageSize);
    pageVO.setPageBlock(pageBlock);        
    pageVO.rowCalculate(nowPage);
            
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("limit", pageSize);
    map.put("offset", pageVO.getStartRow());
    
    List<BoardDTO> boardList=boardSvc.selectBoardList(map);        
    
    int count=boardSvc.selectBoardCount();                
    pageVO.pageCalculate(count);
        
    model.addAttribute("boardList",boardList);
    model.addAttribute("pageVO", pageVO);
                    
    return "board/board";
}
cs

 

3. 게시판 페이지 Service

1
2
3
4
//게시글 리스트
public List<BoardDTO> selectBoardList(Map<String, Object> map) {
    return session.selectList("selectBoardList", map);        
}
cs

 

4. 게시판 페이지 쿼리

1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 게시글 리스트 -->
<select id="selectBoardList" parameterType="java.util.Map" resultType="com.board.BoardDTO">
    SELECT * FROM bbs.bbs
    WHERE bbsAvailable = 1
    ORDER BY bbsID DESC
    LIMIT #{limit} OFFSET #{offset}
</select>
 
<!-- 전체 글 수 조회 -->
<select id="selectBoardCount" resultType="java.lang.Integer">
    SELECT COUNT(*) FROM bbs.bbs
    WHERE bbsAvailable =  1
</select>
cs