20.03 ~ 20.08 국비교육/JSP

MVC 모델2 - 7. 글 보기(Read)

찹키리 2020. 6. 22. 17:23

<글 보기(Read)>

 

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public BoardDataBean getArticle(int num) throws Exception {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    BoardDataBean article = null;
    
    try {
        conn = getConnection();
        pstmt = conn.prepareStatement
        ("update board set readcount = readcount + 1"
        //조회수 증가
                    + "where num = ?");
        pstmt.setInt(1, num);
        //클릭한 글
        pstmt.executeUpdate();
            
        pstmt = conn.prepareStatement
        ("select * from board where num = ?");
        //선택한 글의 모든 행 갖고오기
        pstmt.setInt(1, num);
        rs = pstmt.executeQuery();
        
        if(rs.next()) {
            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"));
            //담는다.
        }
    } 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 article;
    //반환한다.
}

 

조회수 증가, 데이터 갖고오기

 

 

 

 

 

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
package my.action;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import my.board.BoardDBBean;
import my.board.BoardDataBean;
 
public class ContentAction implements CommandAction { 
    //글내용 처리
    public String requestPro(HttpServletRequest request, 
            HttpServletResponse response) throws Throwable {
        
        int num = Integer.parseInt(request.getParameter("num"));
        //해당 글번호
        String pageNum = request.getParameter("pageNum");
        //해당 페이지 번호
        
        BoardDBBean dbPro = BoardDBBean.getInstance();
        //DB처리
        BoardDataBean article = dbPro.getArticle(num);
        //해당 글번호에 대한 해당 레코드
        //DAO에서 가져와서
        
        //해당 뷰에서 사용할 속성
        request.setAttribute("num"new Integer(num));
        request.setAttribute("pageNum"new Integer(pageNum));
        request.setAttribute("article", article);
        //request 영역에 담는다.
        
        return "content.jsp"//해당 뷰
    }
}
 

 

뷰에서 요청을 가져와 DAO에서 해당 데이터를 받아 다시 request 영역으로 보낸다.

 

 

 

 

 

3)View

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<%@ 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>글내용 보기</b></center></br>
<form>
<table width = "500" border = "1" cellspacing = "0" 
cellpadding = "0" align = "center">
<tr height = "30">
<td align = "center" width = "125" bgcolor = "${value_c }">글번호</td>
<td align = "center" width = "125" align = "center">${article.num }</td>
<td align = "center" width = "125" bgcolor = "${value_c }">조회수</td>
<td align = "center" width = "125" align = "center">${article.readcount }</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
<tr>
<td align = "center" width = "125" bgcolor = "${value_c }">글내용</td>
<td align = "left" width = "375" align = "center" colspan = "3">
<pre>${article.content }</pre></td>
</tr>
 
<tr height = "30">
<td colspan = "4" bgcolor = "${value_c }" align = "right">
<input type = "button" value = "글수정"
onclick = 
"document.location.href=
'/boardMvc2Ora/updateForm.do?num=${article.num}&pageNum=${pageNum }'"
>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type = "button" value = "글삭제"
onclick = 
"document.location.href=
'/boardMvc2Ora/deleteForm.do?num=${article.num}&pageNum=${pageNum }'"
>
&nbsp;&nbsp;&nbsp;&nbsp;
<input type = "button" value = "답글쓰기"
onclick = 
"document.location.href=
'/boardMvc2Ora/writeForm.do?num=${article.num}&ref=${article.ref }
&re_step=${article.re_step }&re_level=${article.re_level }'">
&nbsp;&nbsp;&nbsp;&nbsp;
<input type = "button" value = "글목록"
onclick = "document.location.href='/boardMvc2Ora/list.do?pageNum=${pageNum }'"
>
</table>

 

요청에 따라 처리한 결과를 화면에 출력한다.

 

 

 

 

 

매핑

 

 

 

 

 

<실행>

 

 

 

글 목록에서 해당 글 제목을 누르면 글의 내용 등이 출력되고, 조회수가 증가한다.