jspblog : views count

This commit is contained in:
kim
2021-01-22 02:09:33 +09:00
parent 6127b81754
commit 8012cb97a5
3 changed files with 29 additions and 3 deletions

View File

@@ -118,4 +118,24 @@ public class BoardDao {
}
return null;
}
public int updateReadCount(int id) {
String sql = "update board set readCount=readCount+1 where id =?";
Connection conn = DB.getConnection();
PreparedStatement pstmt = null;
if (conn != null) {
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int result = pstmt.executeUpdate();
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
DB.close(conn, pstmt);
}
}
return -1;
}
}

View File

@@ -28,6 +28,7 @@ public class BoardService {
}
public DetailResDto 글상세보기(int id) {
return boardDao.findById(id);
int result = boardDao.updateReadCount(id);
return result == 1 ? boardDao.findById(id) : null;
}
}

View File

@@ -73,8 +73,13 @@ public class BoardController extends HttpServlet {
case "detail" : {
int id = Integer.parseInt(request.getParameter("id"));
DetailResDto dto = boardService.글상세보기(id);
request.setAttribute("detail", dto);
request.getRequestDispatcher("board/detail.jsp").forward(request, response);
if (dto == null) {
Script.back(response, "상세보기에 실패하였습니다.");
} else {
request.setAttribute("detail", dto);
request.getRequestDispatcher("board/detail.jsp").forward(request, response);
}
}
}
}