20.03 ~ 20.08 국비교육/JSP

MVC 모델2 - 6. 글 목록(List)

찹키리 2020. 6. 19. 12:18

<글 목록(List)>

 

1)DAO

 

 

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
//글 개수 구하기
public int getArticleCount() throws Exception {
    //예외발생한 경우 jvm에 던진다.
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    
    int x = 0;
    //행의 개수
    
    try {
        conn = getConnection();
        pstmt = conn.prepareStatement("select count(*) from board");
        rs = pstmt.executeQuery();
        
        if(rs.next()) {
            x = rs.getInt(1);
        }
    } catch(Exception ex) {
        ex.printStackTrace();
    } finally {
        if(rs != nulltry { rs.close(); } catch(SQLException ex) {}
        if(pstmt != nulltry { pstmt.close(); } catch(SQLException ex) {}
        if(conn != nulltry { conn.close(); } catch(SQLException ex) {}
            
    }
    return x;
    //행의 개수 반환
}

 

 

 

 

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
//글목록 구하기
public List getArticles(int start, int end) throws Exception {
    //반환형이 List
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    List articleList = null;
    
    try {
        conn = getConnection();
        
        pstmt = conn.prepareStatement(
        //oracle은 일부만 가져오는 명령이 x -> subquery 사용
            "select a.*"
            + "from ( select ROWNUM as RNUM, b.* "
            + "from ( select * from board order by ref desc, re_step asc) b"
            + ") a "
            //from 뒤에 subquery: inline
            + "where a.RNUM >= ? "
            + "and a.RNUM <= ?");
                
        pstmt.setInt(1, start);
        pstmt.setInt(2, end);
        rs = pstmt.executeQuery();
        
        if(rs.next()) {
            articleList = new ArrayList(end);
            do {
                BoardDataBean article = new BoardDataBean();
                article.setNum(rs.getInt("num"));
                article.setWriter(rs.getString("writer"));
                article.setEmail(rs.getString("email"));
                article.setSubject(rs.getString("subject"));
                article.setPasswd(rs.getString("passwd"));
                article.setReg_date(rs.getTimestamp("reg_date"));
                article.setReadcount(rs.getInt("readcount"));
                article.setRef(rs.getInt("ref"));
                article.setRe_step(rs.getInt("re_step"));
                article.setRe_level(rs.getInt("re_level"));
                article.setContent(rs.getString("content"));
                article.setIp(rs.getString("ip"));
                
                articleList.add(article);
            } while(rs.next());
        }
    } catch(Exception ex) {
        ex.printStackTrace();
    } finally {
        if(rs != nulltry {rs.close();} catch(SQLException ex) {}
        if(pstmt != nulltry {pstmt.close();} catch(SQLException ex) {}
        if(conn != nulltry {conn.close();} catch(SQLException ex) {}
    }
    
    return articleList;
}

 

SQL과 연동, 쿼리를 실행해 데이터베이스와 데이터를 주고 받는다.

 

 

 

 

 

2)Action Class

 

 

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
package my.action;
 
import java.util.Collections;
import java.util.List;
 
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
 
import my.board.BoardDBBean;
 
public class ListAction implements CommandAction{ //글목록 처리
    public String requestPro(HttpServletRequest request,
            HttpServletResponse response) throws Throwable {
        String pageNum = request.getParameter("pageNum");
        //페이지 번호
        
        if(pageNum == null) {
            pageNum = "1";
        }
        
        int pageSize = 10//한 페이지의 글의 개수
        int currentPage = Integer.parseInt(pageNum);
        int startRow = (currentPage - 1* pageSize + 1;
        //한 페이지의 시작글 번호
        int endRow = currentPage * pageSize;
        //한 페이지의 마지막 글번호
        int count = 0;
        int number = 0;
        
        List articleList = null;
        BoardDBBean dbPro = BoardDBBean.getInstance(); //db연동
        count = dbPro.getArticleCount(); //전체 글의 수
        
        if(count > 0) {
            articleList = dbPro.getArticles(startRow, endRow);
            //현재 페이지에 해당하는 글 목록
        } else {
            articleList = Collections.EMPTY_LIST;
        }
        number = count - (currentPage - 1* pageSize;
        //글목록에 표시할 글번호
        
        //해당 뷰에서 사용할 속성
        request.setAttribute("currentPage"new Integer(currentPage));
        request.setAttribute("startRow"new Integer(startRow));
        request.setAttribute("endRow"new Integer(endRow));
        request.setAttribute("count"new Integer(count));
        request.setAttribute("pageSize"new Integer(pageSize));
        request.setAttribute("number"new Integer(number));
        request.setAttribute("articleList", articleList);
        
        return "list.jsp"// 해당 뷰
    }
}

 

request영역에서 가져온 요청을 처리해 뷰로 반환

 

 

 

 

 

3)View

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="/color.jspf" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판</title>
</head>
<body bgcolor = "${bodyback_c }">
<center><b>글목록(전체 글: ${count })</b>
<table width = "700">
<tr>
<td align = "right" bgcolor = "${value_c }">
<a href = "/boardMvc2Ora/writeForm.do">글쓰기</a>
</td>
</tr>

 

 

 

 

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
<c:if test="${count == 0 }">
<table width = "700" border = "1" cellpadding = "0" 
cellspacing = "0">
<tr>
<td align = "center">
게시판에 저장된 글이 없습니다.
</td>
</tr>
</table>
</c:if>
 
<c:if test = "${count > 0 }">
<table width = "700" border = "1" cellpadding = "0" 
cellspacing = "0" align = "center">
<tr height = "30" bgcolor = "${value_c }">
<td align = "center" width = "50">번호</td>
<td align = "center" width = "250">제목</td>
<td align = "center" width = "100">작성자</td>
<td align = "center" width = "150">작성일</td>
<td align = "center" width = "50">조회</td>
<td align = "center" width = "100">IP</td>
</tr>
 
<c:forEach var = "article" items = "${articleList }">
<tr height = "30">
<td align = "center" width = "50">
<c:out value = "${number }" />
<c:set var = "number" value = "${number - 1 }" />
</td>
 
<td width = "250">
<c:if test = "${article.re_level > 0 }">
<img src = "/boardMvc2Ora/img/level.gif" 
width = "${5 * article.re_level }" height = "16">
<img src = "/boardMvc2Ora/img/re.gif">
</c:if>
<c:if test = "${article.re_level == 0 }">
<img src = "/boardMvc2Ora/img/level.gif"
 width = "${5 * article.re_level }" height = "16">
</c:if>
 
<a href = "/boardMvc2Ora/content.do?
num=${article.num }&pageNum=${currentPage}">${article.subject }</a>
<c:if test = "${article.readcount >= 20 }">
<img src = "/boardMvc2Ora/img/hot.gif" border = "0" height = "16">
</c:if>
</td>
<td align = "center" width = "100">
<a href = "mailto:${article.email }">${article.writer}</a>
</td>
<td align = "center" width = "150">${article.reg_date }
</td>
<td align = "center" width = "50">${article.readcount }</td>
<td align = "center" width = "100">${article.ip }</td>
</tr>
</c:forEach>
</table>
</c:if>

 

 

 

 

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
<c:if test = "${count > 0 }">
<c:set var = "pageCount" 
value = "${count / pageSize + (count % pageSize == 0 ? 0 : 1) }" />
<c:set var = "startPage" value = "${currentPage / pageSize + 1 }" />
<c:set var = "endPage" value = "${startPage + 10 }" />
 
<c:if test = "${endPage > pageCoint }">
<c:set var = "endPage" value = "${pageCount }" />
</c:if>
 
<c:if test = "${startPage > 10 }">
<a href = "/boardMvc2Ora/list.do?pageNum=${startPage - 10 }">
[이전]</a>
</c:if>
 
<c:forEach var = "i" begin = "${starPage }" end = "${endPage }">
<a href = "/boardMvc2Ora.list.do?pageNum=${i }">[${i }]</a>
</c:forEach>
 
<c:if test = "${endPage < pageCount }">
<a href = "/boardMvc2Ora/list.do?pageNum=${startPage + 10 }">
[다음]</a>
</c:if>
</c:if>
</center>

 

브라우저에 나타나는 페이지

 

 

 

 

 

매핑

 

 

 

 

 

<실행>

 

 

 

아까 전에 쓴 글이 목록에 잘 나타나고 있다.