전체 방문자
오늘
어제
21종
종이의 코딩 공부방
21종
  • 분류 전체보기 (174)
    • JAVA (64)
    • Springboot (46)
      • 블로그만들기 (45)
    • Database (60)
      • Oracle (60)
    • 프로젝트 3 (CELOVER) (0)
    • 개발서버 구축 (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

인기 글

최근 글

최근 댓글

hELLO · Designed By 정상우.
21종

종이의 코딩 공부방

Springboot/블로그만들기

[Springboot] 블로그 만들기 (28)_글 삭제하기

2023. 12. 1. 14:44

Detail.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%@ include file="../layout/header.jsp"%>

<div class="container">
	<button class="btn btn-secondary" onclick="history.back()">돌아가기</button>
	<button id="btn-update" class="btn btn-warning">수정</button>
	<c:if test="${board.user.id == principal.user.id}"> <!-- 글 작성자만 삭제가능 -->
		<button id="btn-delete" class="btn btn-danger">삭제</button>
	</c:if>
	<br><br>
	<div>
		글 번호 : <span id = "id"><i>${board.id } </i></span>
		작성자 : <span><i>${board.user.userName } </i></span>
	</div>
	<br>
	<div>
		<h3>${ board.title }</h3>
	</div>
	<hr>
	<div>
		<div>${board.content }</div>
	</div>
	<hr>
</div>

<script src="/js/board.js"></script>
<%@ include file="../layout/footer.jsp"%>

board.js

let index = {
	init: function(){
		$("#btn-delete").on("click",()=>{ 
			this.deleteById();
		}); 
	}
	,deleteById: function(){
		let id = $("#id").text();
		
		$.ajax({		
			type:"DELETE",
			url:"/api/board/"+id,
			dataType:"json" 
		}).done(function(resp){
			alert("글삭제가 완료되었습니다.");
			console.log(resp)
			location.href="/";
		}).fail(function(error){
			alert(JSON.stringify(error));
		}); 
	}
}	

index.init();

BoardApiController.java

@DeleteMapping("api/board/{id}")
public ResponseDto<Integer> deleteById(@PathVariable int id){
    boardService.글삭제하기(id);
    return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);  // 1 리턴시 정상
}

BoardService.java

@Transactional
public void 글삭제하기(int id) {
    boardRepository.deleteById(id);
}

 


참고 유튜브 (메타코딩님 강의)

https://youtu.be/s5ZXZliTQq8?si=bAtMbnERNmWErrZ7

    'Springboot/블로그만들기' 카테고리의 다른 글
    • [Springboot] 블로그 만들기 (30)_회원수정(1)
    • [Springboot] 블로그 만들기 (29)_글 수정하기
    • [Springboot] 블로그 만들기 (27)_글 목록 페이징 처리
    • [Springboot] 블로그 만들기 (26)_글 목록 보기
    21종
    21종
    코딩 공부한 것 정리하려고 만든 블로그

    티스토리툴바