jsp
MVC2 패턴 게시판 글 수정 (board_modify)
0304호
2022. 12. 7. 14:25
Board_Modify (게시판 글 수정 페이지)
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ include file="../include/header.jsp" %>
<div align="center" class="div_center">
<h3>게시판 글 수정 페이지</h3>
<hr>
<form action="updateForm.board" method="post">
<table border="1" width="500">
<tr>
<!-- 화면에 보일 필요는 없는데, 데이터를 보내야하는 경우 hidden태그를 사용함 -->
<td>글 번호</td>
<td>${vo.bno }
<input type="hidden" name="bno" value="${vo.bno }">
</td>
</tr>
<tr>
<td>작성자</td>
<td><input type="text" name="writer" value="${vo.writer }" readonly></td>
</tr>
<tr>
<td>글 제목</td>
<td>
<input type="text" name="title" value="${vo.title }">
</td>
</tr>
<tr>
<td>글 내용</td>
<td>
<textarea rows="10" style="width: 95%;" name="content"> ${vo.content }
</textarea>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="수정 하기" onclick="location.href">
<input type="button" value="목록" onclick="location.href='board_list.board'">
</td>
</tr>
</table>
</form>
</div>
<%@ include file="../include/footer.jsp" %>
컨트롤러에 bno를 가져가기 위해 board_content페이지에서 get방식(url?변수명=값)으로 bno를 가져감
<input type="button" value="수정" onclick="location.href='board_modify.board?bno=${vo.bno}'">
컨트롤러에선 BoardServiceImpl에 이미 조회한 글에 대한
정보 조회 메서드(getContent)를 만들어뒀기 때문에 재사용하면 된다.
}else if(command.equals("/board/board_modify.board")) {//수정화면
//조회한글에 대한 정보 조회 재활용
BoardVO vo = service.getContent(request, response);
request.setAttribute("vo", vo);
request.getRequestDispatcher("board_modify.jsp").forward(request, response);
화면에 보일 필요는 없는데, 데이터를 보내야하는 경우 hidden태그를 사용함
<tr>
<td>글 번호</td>
<td>${vo.bno }
<input type="hidden" name="bno" value="${vo.bno }">
</td>
</tr>
Modify에서 보낸 updateForm으로 넘어가는 요청을 컨트롤러에서 잡아서 BoardServiceImpl로 보내줌
}else if(command.equals("/board/updateForm.board")) {
service.update(request, response);
//목록으로 보내기
//response.sendRedirect("board_list.board");
//수정된 글을 바로 보여주기
response.sendRedirect("board_content.board?bno="+request.getParameter("bno"));
}
BoardServiceImpl의 update메서드에서 param에 있는 값을 빼서 DAO의 update로 보내준다
@Override
public void update(HttpServletRequest request, HttpServletResponse response) {
//화면에서 넘어오는 값
String bno = request.getParameter("bno");
String title = request.getParameter("title");
String content = request.getParameter("content");
BoardDAO dao = BoardDAO.getInstance();
dao.update(bno, title, content);
}
BoardDAO의 update()메서드는 받아온 매개변수를 토대로 sql구문을 데이터베이스로 넘겨준다
public void update(String bno, String title, String content) {
String sql = "UPDATE board SET title = ? , content =? WHERE bno = ?";
try {
conn = DriverManager.getConnection(URL, UID, UPW);
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, title);
pstmt.setString(2, content);
pstmt.setString(3, bno);
pstmt.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtill.close(conn, pstmt, rs);
}
}