본 포스팅의 예제는 STS 또는 Eclipse를 사용하지 않고 Intellij를 통해 구현하고 있습니다.
그래서 기존의 STS(Spring Tool Studio)에서 생성된 Spring 프로젝트의 스프링 관련 설정 파일명과
프로젝트 구조가 약간 다를 수 있습니다.Intellij 스프링 mvc 프로젝트 생성 포스팅을 참고해주시면 감사하겠습니다.
Spring-MVC 기본 개념 및 테스트 예제 관련 포스팅 링크
순서 | 포스팅 제목 |
1 | Intellij에서 Spring MVC Project 생성하기 |
2 | Spring MVC - MariaDB 연결테스트 |
3 | Spring MVC - Mybatis 설정 및 테스트 |
4 | SpringMVC 구조 |
5 | SpringMVC + Mybatis |
6 | Spring MVC Controller 작성 연습 |
7 | Spring Interceptor |
Spring-MVC 게시판 예제 이전 포스팅 링크
1. 게시글 수정 구현
1.1 게시글 수정 영속 계층 (Persistence Tier) 구현
src/기본패키지/board/persistence 패키지 안에 BoardDAO 인터페이스와 BoardDAOImpl 클래스 안에 아래와 같이 내용을 추가 해주세요.
// 게시글 수정
public void modifyBoard(BoardVo param);
// 게시글 수정
@Override
@LogException
public void modifyBoard(BoardVo param) {
sqlSession.update(NAMESPACE + ".modifyBoard", param);
}
resources/mappers/board 패키지 안에 BoardSQLMapper.xml 파일 안에 아래와 같이 내용을 추가 해주세요.
<!-- 게시글 수정 -->
<update id="modifyBoard">
update eden_board set category_no = #{category_no}, board_title = #{board_title}, board_content = #{board_content} where board_no = #{board_no}
</update>
1.2 게시글 수정 비지니스 계층 (Business Tier) 구현
src/기본패키지/board/service 패키지 안에 BoardService 인터페이스와 BoardServiceImpl 클래스 안에 아래와 같이 내용을 추가 해주세요.
// 게시글 수정
public void modifyBoard(BoardVo param);
// 게시글 수정
@Override
@LogException
public void modifyBoard(BoardVo param) {
boardDAO.modifyBoard(param);
}
1.3 게시글 수정 컨트롤러 구현
src/기본패키지/board/controller 패키지 안에 BoardController 클래스와 RestBoardController 클래스 안에 아래와 같이 내용을 추가 해주세요.
// 게시글 수정 페이지
@PostMapping(value = "modifyPosting")
public String modifyPosting(@RequestParam(value = "board_no", defaultValue = "0") int boardNo, Model model, @ModelAttribute("boardVo") BoardVo boardVo) {
model.addAttribute("data", boardService.getBoard(boardNo));
model.addAttribute("list", boardService.getCategoryList());
return "board/modifyPosting";
}
// 게시글 수정 프로시져
@PostMapping(value = "modifyPostingProcess")
@LogException
public HashMap<String, Object> modifyPostingProcess(@Valid BoardVo param, BindingResult result) {
if (result.hasErrors()) {
data.put("result", "error");
}
boardService.modifyBoard(param);
data.put("result", "success");
return data;
}
1.4 게시글 수정 JSP 구현
web-inf/views/board 패키지 안에 modifyPosting.jsp 파일 생성후 아래와 같이 작성 해주세요.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../include/head.jsp" %>
<%@ include file="../include/user_menu.jsp" %>
<html>
<body class="hold-transition skin-green-light sidebar-mini" oncopy="return false" oncut="return false"
onpaste="return false">
<div class="wrapper">
<%@ include file="../include/top_menu.jsp" %>
<%@ include file="../include/left_menu.jsp" %>
<div class="content-wrapper">
<section class="content container-fluid">
<div class="col-lg-12">
<form name="detailsForm" role="form" method="post">
<input type="hidden" id="boardNo" name="board_no" value="">
</form>
<form:form id="modifyForm" method="post" action="${path}/board/modifyPostingProcess"
modelAttribute="boardVo">
<div class="box box-primary">
<div class="box box-header with-border">
<h3 class="box-title">게시글 수정</h3>
</div>
<div class="box-body">
<input type="hidden" id="updateBoardNo" name="board_no" value="${data.boardVo.board_no}">
<div class="row mt-1">
<div class="col-lg-5">
<label for="categoryList">카테고리</label>
</div>
</div>
<div class="row mt-1">
<div class="col-lg-5">
<select name="category_no" class="form-control" id="categoryList">
<c:forEach items="${list}" var="category">
<c:choose>
<c:when test="${data.boardVo.category_no == category.category_no}">
<option value="${category.category_no}" selected>
${category.category_name}
</option>
</c:when>
<c:otherwise>
<option value="${category.category_no}">
${category.category_name}
</option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>
</div>
</div>
<div class="row mt-1">
<div class="col-lg-5">
<label for="title">제목</label>
</div>
</div>
<div class="row mt-1">
<div class="col-lg-5">
<form:input path="board_title" id="title" value="${data.boardVo.board_title}"/>
</div>
</div>
<div class="row mt-1">
<div class="col my-auto"><form:errors path="board_title" id="error_message"/></div>
</div>
<div class="row mt-1">
<label for="content">내용</label>
</div>
<div class="row mt-1">
<div class="col-lg-5">
<textarea path="board_content" class="form-control" id="content" rows="30"
placeholder="내용을 입력해주세요"
style="resize: none;" >${data.boardVo.board_content}</textarea>
</div>
</div>
</div>
<div class="box-footer">
<button type="button" class="btn btn-primary listBtn"><i class="fa fa-list"></i> 목록</button>
<div class="pull-right">
<button type="button" class="btn btn-warning cancelBtn"><i class="fa fa-trash"></i> 취소
</button>
<button type="button" class="btn btn-success crystalBtn"><i class="fa fa-save"></i> 수정
저장
</button>
</div>
</div>
</div>
</form:form>
</div>
</section>
</div>
<%@ include file="../include/footer.jsp" %>
</div>
<%@ include file="../include/plugin_js.jsp" %>
</body>
</html>
1.5 게시글 수정 JavaSccipts 구현
webapp/resources/dist/js/board 패키지 안에 relatedBulletin.js 파일에 아래와 같이 내용을 추가 해주세요.
$(".crystalBtn").click(function () {
$.ajax({
type: "post",
url: "../board/modifyPostingProcess",
data: {
board_no: $("#updateBoardNo").val(),
category_no: $("#categoryList").val(),
board_title: $("#title").val(),
board_content: $("#content").val()
},
dataType: "json",
success: function (data) {
if (data.result == "error") {
location.reload();
} else {
alert("게시글 수정에 성공 하였습니다.");
goPage($("#updateBoardNo").val());
}
}
})
});
1.6 게시글 수정 화면 확인
2. 게시글 삭제 구현
2.1 게시글 삭제 영속 계층 (Persistence Tier) 구현
src/기본패키지/board/persistence 패키지 안에 BoardDAO 인터페이스와 BoardDAOImpl 클래스 안에 아래와 같이 내용을 추가 해주세요.
// 게시글 삭제
public void deletePosting(int board_no);
// 게시글 삭제
@Override
@LogException
public void deletePosting(int boardNo) {
sqlSession.delete(NAMESPACE + ".deletePosting", boardNo);
}
2.2 게시글 삭제 비지니스 계층 (Business Tier) 구현
src/기본패키지/board/service 패키지 안에 BoardService 인터페이스와 BoardServiceImpl 클래스 안에 아래와 같이 내용을 추가 해주세요.
// 게시글 삭제
public void deletePosting(int boardNo);
// 게시글 삭제
@Override
@LogException
public void deletePosting(int boardNo) {
// 게시글 조회수 중복 체크 삭제
boardDAO.deleteViewPage(boardNo);
// 게시글 삭제
boardDAO.deletePosting(boardNo);
}
2.3 게시글 삭제 컨트롤러 구현
src/기본패키지/controller 패키지 안에 RestBoardController 클래스 안에 아래와 같이 내용을 추가 해주세요.
// 게시글 삭제 프로시저
@PostMapping(value = "deletePosting")
@LogException
public HashMap<String, Object> deletePosting(int boardNo) {
data.put("result", "success");
boardService.deletePosting(boardNo);
return data;
}
2.4 게시글 삭제 JavaScripts 구현
webapp/resources/dist/js/relatedBulletin.js 파일 안에 아래와 같이 내용을 추가 해주세요
$(".delBtn").click(function () {
if (confirm("해당 게시글을 정말로 삭제 하시겠습니까??")) {
$.ajax({
type: "post",
url: "../board/deletePosting",
data: {
boardNo : $("#boardNo").val()
},
dataType: "json",
success: function (data) {
if (data.result == "success") {
alert("게시글 삭제에 성공 하였습니다.");
postingList($("#categoryNo").val());
}
}
})
}
});
2.5 게시글 삭제 화면 확인
'스프링 프레임워크 > 스프링 MVC' 카테고리의 다른 글
# Spring MVC 게시판 예제 19 : 게시글 북마크 (2) | 2023.03.19 |
---|---|
# Spring MVC 게시판 예제 18 : 게시글 좋아요 (0) | 2023.03.15 |
# Spring MVC 게시판 예제 16 : 게시글 상세보기 (0) | 2023.03.10 |
# Spring MVC 게시판 예제 15 : 게시글 작성 (0) | 2023.03.09 |
# Spring MVC 게시판 예제 14 : 게시글 목록 출력 (0) | 2023.03.07 |
댓글