📝 게시글 삭제 기능
💡 VS Code
🔎 board.js
(function(){
const deleteBtn = document.getElementById("deleteBtn");
if(deleteBtn != null){
deleteBtn.addEventListener("click", function(){
let url = "delete";
const params = new URL(location.href).searchParams;
const no = "?no=" + params.get("no");
const type = "&type=" + params.get("type");
url += no + type;
if(confirm("정말로 삭제하시겠습니까?")){
location.href = url;
}
})
}
})();
💡 Eclipse
🔎 BoardDeleteServlet.java
package edu.kh.community.board.controller;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import edu.kh.community.board.model.service.BoardService;
@WebServlet("/board/delete")
public class BoardDeleteServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
int type = Integer.parseInt(req.getParameter("type"));
int boardNo = Integer.parseInt(req.getParameter("no"));
int result = new BoardService().deleteBoard(boardNo);
HttpSession session = req.getSession();
String path = null;
String message = null;
if(result > 0) {
message = "게시글이 삭제되었습니다.";
path = "list?type=" + type;
} else {
message = "게시글 삭제에 실패했습니다.";
path = req.getHeader("referer");
}
session.setAttribute("message", message);
resp.sendRedirect(path);
} catch (Exception e) {
e.printStackTrace();
}
}
}
🔎 BoardService.java
public int deleteBoard(int boardNo) throws Exception {
Connection conn = getConnection();
int result = dao.deleteBoard(conn, boardNo);
if(result > 0) commit(conn);
else rollback(conn);
close(conn);
return result;
}
🔎 BoardDAO.java
public int deleteBoard(Connection conn, int boardNo) throws Exception {
int result = 0;
try {
String sql = prop.getProperty("deleteBoard");
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, boardNo);
result = pstmt.executeUpdate();
} finally {
close(pstmt);
}
return result;
}
🔎 board-sql.xml
<!-- 게시글 삭제 -->
<entry key="deleteBoard">
UPDATE BOARD SET
BOARD_ST = 'Y'
WHERE BOARD_NO = ?
</entry>