Servlet게시판5: 글 수정하기

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");
// BoardUpdateProAction 객체 생성
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() 실행됨");
//1. 한글처리
req.setCharacterEncoding("UTF-8");

//2.전달되는 파라미터 정보저장
int bno = Integer.parseInt(req.getParameter("bno"));
String pageNum = (String) req.getAttribute("pageNum");

//3.DAO객체생성 -> bno 해당되는 정보 가져오기
BoardDAO bdao = new BoardDAO();
BoardDTO bdto = bdao.getBoard(bno);

//5.리퀘스트영역에 정보저장
req.setAttribute("bdto", bdto);

//6. 화면출력하는 뷰페이지(.jsp)로 전달 (get방식)
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");
// 전달되는값들 저장 (이름,비밀번호,제목, 내용, bno,pageNum)

String pageNum = request.getParameter("pageNum");
// BoardDTO 객체 생성
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 객체 생성 -> 정보 수정 메서드 updateBoard(dto)
BoardDAO bdao = new BoardDAO();

int result = bdao.updateBoard(bdto);

System.out.println("M : DB 처리 완료후 결과 -> "+result);

// 결과를 리턴받아서 처리 (1, 0 ,-1)
// 페이지이동 (js)

// 페이지 출력준비
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
//특정글만 조회수 1 증가
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
// getBoard(bno)
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;
}
// getBoard(bno)




updateForm.jsp 생성

  • 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>