💡 핵심 요약 (TL;DR)
Servlet게시판4: 글 내용보기의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
핵심 설정과 구현 코드를 단계별로 파악하여 상황에 맞게 유연하게 활용할 수 있다.
content.jsp 코드 변경
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
| <%
BoardDTO bdto = (BoardDTO) request.getAttribute("bdto"); String pageNum = (String) request.getAttribute("pageNum"); %>
<fieldset> <legend>글 내용 보기</legend> <table border="solid,1px"> <tr> <th>글번호</th> <td><%=bdto.getBno()%></td> <th>조회수</th> <td><%=bdto.getReadcount()%></td> </tr> <tr> <th>작성자</th> <td><%=bdto.getName()%></td> <th>작성일</th> <td><%=bdto.getDate()%></td> </tr> <tr> <th>제목</th> <td colspan="3"><%=bdto.getSubject()%></td> </tr> <tr> <th>첨부파일</th> <td colspan="3"><a href="D:\upfile\<%=bdto.getFile()%>"><%=bdto.getFile() %></a></td> </tr> <tr> <th>내용</th> <td colspan="3" height="300px"><%=bdto.getContent()%></td> </tr> <tr> <td colspan="4" style="text-align:center"> <% %> <input type="button" value="글수정" class="btn" onclick="location.href='./BoardUpdate.bo?bno=<%=bdto.getBno()%>&pageNum=<%=pageNum%>'"> <input type="button" value="글삭제" class="btn" onclick="location.href='./BoardDelete.bo?bno=<%=bdto.getBno()%>&pageNum=<%=pageNum%>'"> <input type="button" value="답글쓰기" class="btn" onclick="location.href='./BoardReWrite.bo?bno=<%=bdto.getBno()%>&re_ref=<%=bdto.getRe_ref() %>&re_lev=<%=bdto.getRe_lev()%>&re_seq=<%=bdto.getRe_seq()%>'"> <% %> <input type="button" value="목록으로" class="btn" onclick="location.href='./BoardList.bo?pageNum=<%=pageNum%>'"> <!-- location.href='boardList.jsp만 하면 5페이지보고있다가 다시 1페이지로 돌아가버린다 이때 historyback하면 조회수가 올라가지않는다. 따라서 pageNum을 가져와서 사용하면된다 --> </td> </tr> </table> </fieldset>
|
BoardFrontController.java의 doProcess()의 주소비교 후 처리부분에 코드 추가
1 2 3 4 5 6 7 8 9 10
| }else if(command.equals("./BoardContent.bo")){ System.out.println("컨트롤러 : BoardContent객체 생성 execute() 호출"); action = new BoardContentAction(); try{ forward = action.execute(request, response); }catch(Exception e){ e.printStackTrace(); } }
|
BoardContentAction.java 생성
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
| public class BoardContentAction implements Action {
@Override public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { System.out.println("모델: BoardContentAction안의 execute() 실행됨"); req.setCharacterEncoding("UTF-8"); int bno = Integer.parseInt(req.getParameter("bno")); String pageNum = req.getParameter("pageNum"); BoardDAO bdao = new BoardDAO(); bdao.updateReadCount(bno); BoardDTO bdto = bdao.getBoard(bno); req.setAttribute("bdto", bdto); req.setAttribute("pageNum", pageNum); ActionForward forward = new ActionForward(); forward.setPath("./baord/content.jsp"); forward.setRedirect(false); return forward; } }
|
BoardDAO.java에 updateReadCount()메서드 작성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| public void updateReadCount(int bno){ try { getCon(); sql="update itwill_board set readcount=readcount+1 where bno=?"; pstmt.setInt(1, bno); pstmt = con.prepareStatement(sql); pstmt.executeUpdate(); System.out.println("조회수1증가"); } catch (Exception e) { e.printStackTrace(); } finally { closeDB(); } }
|
BoardDAO.java에 getBoard()메서드 작성
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
| public BoardDTO getBoard(int bno) { BoardDTO bdto = null; try { getCon(); sql ="select * from itwill_board where bno=?"; pstmt = con.prepareStatement(sql); pstmt.setInt(1, bno); rs = pstmt.executeQuery(); if(rs.next()){ bdto = new BoardDTO(); bdto.setBno(rs.getInt("bno")); bdto.setContent(rs.getString("content")); bdto.setDate(rs.getDate("date")); bdto.setFile(rs.getString("file")); bdto.setIp(rs.getString("ip"));
bdto.setName(rs.getString("name")); bdto.setPw(rs.getString("pw")); bdto.setRe_lev(rs.getInt("re_lev")); bdto.setRe_ref(rs.getInt("re_ref")); bdto.setRe_seq(rs.getInt("re_seq"));
bdto.setReadcount(rs.getInt("readcount")); bdto.setSubject(rs.getString("subject")); } System.out.println("DAO: 글정보저장완료"); } catch (Exception e) { e.printStackTrace(); } finally { closeDB(); } return bdto; }
|
#TILITWILL(JAVA)