💡 핵심 요약 (TL;DR)
Servlet게시판5: 글 수정하기의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* hidden타입 : 화면에는 보이지않지만 데이터 저장 및 전달이 가능해서 데이터 이동용으로 주로 사용을 적용할 수 있다.
BoardFrontController.java의 doProcess()의 주소비교 후 처리부분에 코드 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| }else if(command.equals("/BoardUpdate.bo")){ System.out.println("컨트롤러 : BoardUpdate객체 생성 execute() 호출"); action = new BoardUpdateAction(); try{ forward = action.execute(request, response); }catch(Exception e){ e.printStackTrace(); } }else if(command.equals("/BoardUpdateProAction.bo")){ System.out.println("C : /BoardUpdateProAction.bo 호출"); System.out.println("C : .jsp -> .bo"); action = new BoardUpdateProAction(); try { forward = action.execute(request, response); } catch (Exception e) { e.printStackTrace(); }
|
BoardUpdateAction.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
| public class BoardUpdateAction implements Action {
@Override public ActionForward execute(HttpServletRequest req, HttpServletResponse resp) throws Exception { System.out.println("모델: BoardUpdateAction안의 execute() 실행됨"); req.setCharacterEncoding("UTF-8"); int bno = Integer.parseInt(req.getParameter("bno")); String pageNum = (String) req.getAttribute("pageNum"); BoardDAO bdao = new BoardDAO(); BoardDTO bdto = bdao.getBoard(bno); req.setAttribute("bdto", bdto); ActionForward forward = new ActionForward(); forward.setPath("./board/updateForm.jsp?pageNum="+pageNum); forward.setRedirect(false); return forward; }
}
|
BoardUpdateProAction.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 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 59 60 61
| public class BoardUpdateProAction implements Action {
@Override public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("M : BoardUpdateProAction_execute 호출"); request.setCharacterEncoding("UTF-8"); String pageNum = request.getParameter("pageNum"); BoardDTO bdto = new BoardDTO(); bdto.setBno(Integer.parseInt(request.getParameter("bno"))); bdto.setName(request.getParameter("name")); bdto.setPw(request.getParameter("pw")); bdto.setSubject(request.getParameter("subject")); bdto.setContent(request.getParameter("content")); BoardDAO bdao = new BoardDAO(); int result = bdao.updateBoard(bdto); System.out.println("M : DB 처리 완료후 결과 -> "+result); response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); if(result == 0){ out.print("<script>"); out.print(" alert('비밀번호 오류');"); out.print(" history.back();"); out.print("</script>"); out.close(); return null; }else if(result == -1){ out.print("<script>"); out.print(" alert('해당 글 없음!');"); out.print(" history.back();"); out.print("</script>"); out.close(); return null; } out.print("<script>"); out.print(" alert('글 수정완료!');"); out.print(" location.href='./BoardList.bo?pageNum="+pageNum+"';"); out.print("</script>"); out.close(); return null; } }
|
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 = con.prepareStatement(sql); pstmt.setInt(1, bno); pstmt.executeUpdate(); System.out.println("DAO: 조회수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 35 36 37 38 39 40
| 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; }
|
- hidden타입 : 화면에는 보이지않지만 데이터 저장 및 전달이 가능해서 데이터 이동용으로 주로 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| <% String pageNum = (String) request.getParameter("pageNum"); BoardDTO bdto = (BoardDTO) request.getAttribute("bdto"); %> <fieldset> <legend>게시판 글쓰기</legend> <form action="./BoardUpdateProAction.bo?pageNum=<%=pageNum %>" method="post" name="fr"> <input type="hidden" name="bno" value="<%=bdto.getBno() %>"> 글쓴이 : <input type="text" name="name" required value="<%=bdto.getName() %>"><br> 비밀번호 : <input type="password" name="pw" required ><br> 제목 : <input type="text" name="subject" required value="<%=bdto.getSubject() %>"><br> 내용 : <br> <textarea rows="10" cols="35" name="content" required><%=bdto.getContent() %></textarea><br> <input type="submit" value="글 수정하기" class="btn"> <input type="button" value="수정취소" class="btn" onclick="location.href='./BoardList.bo'"> </form> </fieldset>
|
#TILITWILL(JAVA)