수업(국비지원)/JSP
[JSP] MVC MODEL2 방식 - 게시물 상세보기, 조회수 증가(info, BoardDao, BoardController)
byeolsub
2023. 4. 20. 23:45
📌 info.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%-- /jsp3/src/main/webapp/view/board/info.jsp --%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>게시물 상세 보기</title>
</head>
<body>
<hr>
<div class="container">
<h2 id="center">게시물 상세 보기</h2>
<table class="table table-hover">
<tr><th>작성자</th><td style="text-align:left;">${b.writer}</td>
<tr><th>등록일</th><td style="text-align:left;">${b.regdate}</td></tr>
<tr><th>제목</th><td colspan="3">${b.subject}</td></tr>
<tr><th>내용</th><td colspan="3">${b.content}</td></tr>
<tr><th>파일</th><td colspan="3">${b.file1}</td></tr>
</table>
<div id="center" style="padding:3px;">
<button class="btn btn-dark" onclick="location.href='replyForm?num=${b.num}'">답변</button>
<button class="btn btn-dark" onclick="location.href='updateForm?num=${b.num}'">수정</button>
<button class="btn btn-dark" onclick="location.href='deleteForm?num=${b.num}'">삭제</button>
<button class="btn btn-dark" onclick="location.href='list'">목록보기</button>
</div>
</body>
</html>
📌 BoardController.java 추가
@RequestMapping("info")
public String info (HttpServletRequest request, HttpServletResponse response) {
//num : 게시물 번호. 파라미터값 저장
int num = Integer.parseInt(request.getParameter("num"));
//num에 해당하는 정보를 db에서 읽어서 Board 객체에 저장
Board b = dao.selectOne(num);
//조회수 증가
dao.readcntAdd(num);
//View에 b객체를 전달. request객체에 속성 등록
request.setAttribute("b",b);
return "/view/board/info.jsp";
}
📌 BoardDao.java 추가
//조회수 1 증가
public void readcntAdd(int num) {
String sql = "update board set readcnt = readcnt+1 where num=?";
Connection conn = DBConnection.getConnection();
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,num);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBConnection.close(conn,pstmt,null);
}
}