위로
아래
글 수정 매핑
글 수정 HTML
글쓰기 jsp와 똑같이 하되, 게시글 보기처럼 모든 데이터를 불러와 input의 value에 적어놓는다.
수정할 수 있도록 readonly는 제거한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>글보기</title>
<link rel="stylesheet" type="text/css" href="css/form.css">
<script src="jquery/jquery-3.7.0.min.js"></script>
<script type="text/javascript">
$().ready(function(){
$("#submit1").on("click",function(){
if(validate()){
$("form[name=updateform]").submit();
}
});
});
function validate(){
let f = $("form[name=updateform] .chk");
let flen = f.length;
for(let i=0;i<flen;i++){
if(f.eq(i).val()==null ||
f.eq(i).val().length==0 ||
f.eq(i).val().trim()==""){
alert(f.eq(i).attr("title")+"의 입력은 필수입니다.");
f.eq(i).focus();
return false;
}
}
return true;
}
</script>
</head>
<body>
<form name="updateform" action="updateProc.do" method="post">
<table>
<tr>
<th>제목</th>
<td><input type="text" name="subject" class="chk" title="제목" value="${article.subject}"></td>
</tr>
<tr>
<th>작성자</th>
<td><input type="text" name="writer" class="chk" title="작성자" value="${article.writer}"></td>
</tr>
<tr>
<th>내용</th>
<td><textarea name="content" class="chk" title="내용">${article.content}</textarea></td>
</tr>
<tr>
<th>비밀번호</th>
<td><input type="password" name="pw" class="chk" title="비밀번호" value="${article.passwd}"></td>
</tr>
<tr>
<td colspan="3">
<input type="button" class="btn" value="글 수정" id="submit1">
<input type="button" class="btn" value="글목록" onclick="location.href='list.do'">
</td>
</tr>
</table>
<input type="hidden" name="bno" value="${article.bno}">
<input type="hidden" name="bref" value="${article.bref}">
<input type="hidden" name="bstep" value="${article.bstep}">
<input type="hidden" name="blevel" value="${article.blevel}">
</form>
</body>
</html>
글 수정 불러오기 ACTION
글 수정하기는 글보기 페이지처럼 해당 게시글의 모든 데이터를 불러와야한다. 따라서 글수정 페이지에 가기 전에 파라미터를 DTO에 저장하고 DTO를 설정해서 넘겨줄 action이 필요하다.
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.BoardDAO;
import model.BoardDTO;
public class UpdateAction implements CommandAction {
@Override
public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
BoardDTO dto = new BoardDTO();
dto.setBno(Integer.parseInt(request.getParameter("bno")));
dto.setBref(Integer.parseInt(request.getParameter("bref")));
dto.setBstep(Integer.parseInt(request.getParameter("bstep")));
dto.setBlevel(Integer.parseInt(request.getParameter("blevel")));
dto.setSubject(request.getParameter("subject"));
dto.setWriter(request.getParameter("writer"));
dto.setContent(request.getParameter("content"));
dto.setPasswd(request.getParameter("pw"));
request.setAttribute("article", dto);
return "view/UpdateView.jsp";
}
}
글 수정 작동 ACTION
수정된 글의 데이터를 DTO에 저장해 DAO에게 전달한다.
DAO는 게시글을 UPDATE하기 때문에 딱히 불러올 정보는 없고, 성공했는지 실패했는지 결과를 알려주면 MsgPage를 통해 출력한다.
package action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.BoardDAO;
import model.BoardDTO;
public class UpdateProc implements CommandAction {
@Override
public String requestProc(HttpServletRequest request, HttpServletResponse response) throws Exception {
BoardDTO dto = new BoardDTO();
int result = 0;
String msg = null;
String page = null;
dto.setBno(Integer.parseInt(request.getParameter("bno")));
dto.setBref(Integer.parseInt(request.getParameter("bref")));
dto.setBstep(Integer.parseInt(request.getParameter("bstep")));
dto.setBlevel(Integer.parseInt(request.getParameter("blevel")));
dto.setSubject(request.getParameter("subject"));
dto.setWriter(request.getParameter("writer"));
dto.setContent(request.getParameter("content"));
dto.setPasswd(request.getParameter("pw"));
BoardDAO dao = BoardDAO.getInstance();
result = dao.UpdateArticle(dto);
if(result>0) {
msg = "수정 성공";
} else {
msg = "수정 실패";
}
page = "content.do?bno="+dto.getBno();
request.setAttribute("msg", msg);
request.setAttribute("page", page);
return "view/MsgPage.jsp";
}
}
글 수정 DAO
public int UpdateArticle(BoardDTO dto) {
Connection conn = null;
PreparedStatement pstmt = null;
int r = 0;
try {
conn = DBConnector.getInstance().getConnection();
String sql = "UPDATE board SET "
+ "subject = ?, writer = ?, content = ?, passwd = ? "
+ "WHERE bno = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, dto.getSubject());
pstmt.setString(2, dto.getWriter());
pstmt.setString(3, dto.getContent());
pstmt.setString(4, dto.getPasswd());
pstmt.setInt(5, dto.getBno());
r = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(conn!=null) conn.close();
if(pstmt!=null) pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return r;
}
글 수정 매핑
/list.do=action.ListAction
/writeForm.do=action.WriteFormAction
/writeAction.do=action.WriteAction
/content.do=action.ContentAction
/update.do=action.UpdateAction
/updateProc.do=action.UpdateProc