JSP/model 1

게시판 생성 (3) - 상세 페이지

Kiwisae 2022. 9. 7. 15:11
  • DBCP 방식 사용,
  • 액션태그 사용 (useBean, setProperty)
  • DTO, DAO 클래스
  • 페이징 처리 (inline view)

 

 

프로그램 구조

 

Java Resources >
src > board
DTO 클래스 BoardDataDTO.java  
DAO 클래스 BoardDBBean.java  
WebContent > board board writeForm.jsp  
writePro.jsp  
list.jsp  
content.jsp  
updateForm.jsp  
updatePro.jsp  
deleteFrom.jsp  
deletePro.jsp  
META-INF context.xml 커넥션 풀 환경설정 파일
WEB-INF > lib ojdbc6.jar  
  web.xml 프로젝트 환경설정 파일

 

 

 

 

 

1. list.jsp  수정

 

게시판 목록의 글제목을 클릭하면 글 내용이 보이도록 한다.

board.getSubject() 양 쪽에 앵커 태그를 추가해서 클릭하면 content.jsp로 이동하도록 설정한다.

 

		<td><a href="content.jsp?num=<%= board.getNum() %>&Page=<%= currentPage %>"
			style="text-decoration:none">
			<%= board.getSubject() %></a></td>

 

 

 

2. content.jsp 생성 후 DAO 연결

 

그리고 클릭시 조회수가 1 증가하고 글 내용을 구해오는 메소드 iupdateContent를 생성한다.

이 메소드는 num 값을 매개로 내용을 실행한다.

 

	int num = Integer.parseInt(request.getParameter("num"));
	String nowpage = request.getParameter("page");
	BoardDBBean dao = BoardDBBean.getInstance();
    
	BoardDataBean board = dao.updateContent(num);

 

 

 

3. DAO 에서 updateContent 메소드 생성

 

 

	public BoardDataBean updateContent(int num) {
		BoardDataBean board = new BoardDataBean();
		
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		try {
			con = getConnection();	// DB접속
			
			String sql = "update board0 set readcount=readcount+1 ";
				sql += " where num=?";
				
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, num);
			pstmt.executeQuery();
			
			sql="select * from board0 where num=?";
			
			pstmt.setInt(1,  num);
			rs = pstmt.executeQuery();
			
			if(rs.next()) {
				board.setNum(rs.getInt("num"));
				board.setWriter(rs.getString("writer"));
				board.setEmail(rs.getString("email"));
				board.setSubject(rs.getString("subject"));
				board.setPasswd(rs.getString("passwd"));
				board.setReg_date(rs.getTimestamp("reg_date"));
				board.setReadcount(rs.getInt("readcount"));
				board.setContent(rs.getString("content"));
				board.setIp(rs.getString("ip"));
			} // if end
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(rs != null) try { rs.close(); }catch(Exception e) {}
			if(pstmt != null) try { pstmt.close(); }catch(Exception e) {}
			if(con != null) try { con.close(); }catch(Exception e) {}
		}
		return board;
	}	// updateContent end

 

 

 

 

4. content.jsp의 DAO 연결 코드 하단에 상세 페이지의 틀을 생성하고 값을 가져온다.

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시판 - 상세 페이지</title>
</head>
<body>
<table border=1 width=500 align=center bgcolor="#ECFFFF">
	<caption>상 세 페 이 지</caption>
	<tr>
		<td>글번호</td>
		<td><%= board.getNum() %></td>
		<td>조회수</td>
		<td><%= board.getReadcount() %></td>
	</tr>
	<tr>
		<td>작성자</td>
		<td><%= board.getWriter() %></td>
		<td>작성일</td>
		<td><%= board.getReg_date() %></td>
	</tr>
	<tr>
		<td>글제목</td>
		<td colspan=3><%= board.getSubject() %></td>
	</tr>
	<tr>
		<td>글내용</td>
		<td colsapn=3><%= board.getContent() %></td>
	</tr>
	<tr>
		<td colspan=4 align=center>
			<input type="button" value="글수정">
			<input type="button" value="글삭제">
			<input type="button" value="글목록">
		</td>
	</tr>
</table>
</body>
</html>