YeaLow
article thumbnail

๐Ÿค๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ

 

1. Index.jsp ๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ ๋ฒ„ํŠผ์— <a>ํƒœ๊ทธ ์ถ”๊ฐ€

 

2. BoardController ์ž‘์„ฑ

//๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ
	@GetMapping("/board/{id}")
	public String findById(@PathVariable int id, Model model) {
		model.addAttribute("board", boardService.๊ธ€์ƒ์„ธ๋ณด๊ธฐ(id));
		return "board/detail";
	}

 

3. BoardService ์ž‘์„ฑ

public Board ๊ธ€์ƒ์„ธ๋ณด๊ธฐ(int id) {
    return boardRepository.findById(id)
        .orElseThrow(()->{
            return new IllegalArgumentException("๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ ์‹คํŒจ: ์•„์ด๋””๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
        });
}

 

4. detail.jsp ์ž‘์„ฑ

  • ์ž‘์„ฑ์ž ๋ณธ์ธ๋งŒ ์ˆ˜์ •, ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„๋ก(์ž‘์„ฑ์ž์™€ ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž ์•„์ด๋””๊ฐ€ ๊ฐ™์„ ๋•Œ) <c:if> ์กฐ๊ฑด๋ฌธ์„ ๋„ฃ์—ˆ๋‹ค.
<%@ 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>
		
		<c:if test="${board.user.id == principal.user.id }">
			<a  href="/board/${board.id }/updateForm" class="btn btn-warning">์ˆ˜์ •</a>
			<button id="btn-delete" class="btn btn-danger">์‚ญ์ œ</button>
		</c:if>
		<br>
		
		<div>
			๊ธ€๋ฒˆํ˜ธ: <span id="id"><i>${board.id } </i></span>
			์ž‘์„ฑ์ž: <span><i>${board.user.username} </i></span>
		</div>
		
		<div class="form-group">
			<h3>${board.title }</h3>
		</div>
		
		<hr/>
		
		<div class="form-group">
			<div>${board.content }</div>
		</div>
		<hr/>
</div>

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

 

5. ๊ธ€ ์ƒ์„ธ๋ณด๊ธฐ ๊ตฌํ˜„ ์™„๋ฃŒ


๐Ÿค๊ธ€ ์‚ญ์ œํ•˜๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ

 

1. ๊ธ€ ์‚ญ์ œ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์‚ญ์ œ๋˜๋„๋ก ์ฝ”๋“œ ์ถ”๊ฐ€

let index = {
	init: function() {
		$("#btn-save").on("click", () => {
			this.save();
		});

		$("#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));
		});

	}
 }

 

2. BoardApiController ์ž‘์„ฑ

	@DeleteMapping("/api/board/{id}")
	public ResponseDto<Integer> deleteById(@PathVariable int id){
		boardService.๊ธ€์‚ญ์ œํ•˜๊ธฐ(id);
		
		return new ResponseDto<Integer>(HttpStatus.OK.value(),1);
	}

 

3. BoardService ์ž‘์„ฑ

	@Transactional
	public void ๊ธ€์‚ญ์ œํ•˜๊ธฐ(int id) {
		boardRepository.deleteById(id);
	}

 

4. ์‚ญ์ œ ์™„๋ฃŒ


๐Ÿค๊ธ€ ์ˆ˜์ •ํ•˜๊ธฐ ๊ตฌํ˜„ํ•˜๊ธฐ

 

1. BoardController ์ž‘์„ฑ

//๊ธ€ ์ˆ˜์ •ํ•˜๊ธฐ
	@GetMapping("/board/{id}/updateForm")
	public String updateForm(@PathVariable int id, Model model) {
		model.addAttribute("board",boardService.๊ธ€์ƒ์„ธ๋ณด๊ธฐ(id));
		return "board/updateForm";
	}

 

2. ์ˆ˜์ •ํ•˜๊ธฐ๋Š” ๋ฒ„ํŠผ ๋Œ€์‹  <a>๋งํฌ๋ฅผ ๊ฑธ์–ด๋‘”๋‹ค.

 

3. update ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ ์ž‘์„ฑ

let index = {
	init: function() {
		$("#btn-save").on("click", () => {
			this.save();
		});

		$("#btn-delete").on("click", () => {
			this.deleteById();
		});

		$("#btn-update").on("click", () => {
			this.update();
		});
	},

	update: function() {

		let id = $("#id").val();

		let data = {
			title: $("#title").val(),
			content: $("#content").val()
		};

		$.ajax({
			type: "PUT",
			url: "/api/board/"+id,
			data: JSON.stringify(data),
			contentType: "application/json; charset=utf-8",
			dataType: "json"

		}).done(function(resp) {
			alert("๊ธ€ ์ˆ˜์ •์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");
			location.href = "/";

		}).fail(function(error) {
			alert(JSON.stringify(error));
		});

	}
}

 

4. updateForm.jsp ์ž‘์„ฑ

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

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

<div class="container">
	<form>
	<input type="hidden" id="id" value="${board.id }">
		<div class="form-group">
			<input type="text" class="form-control"  value="${board.title }" placeholder="Enter title" id="title">
		</div>
		<div class="form-group">
			<textarea class="form-control summernote" rows="5" id="content">${board.content }</textarea>
		</div>
	</form>
	<button id="btn-update" class="btn btn-primary">๊ธ€์ˆ˜์ • ์™„๋ฃŒ</button>
</div>

<script>
      $('.summernote').summernote({
        tabsize: 2,
        height: 300
      });
</script>
<script src="/js/board.js"></script>
<%@include file="../layout/footer.jsp"%>

 

5. BoardApiController ์ž‘์„ฑ

	@PutMapping("/api/board/{id}")
	public ResponseDto<Integer> update(@PathVariable int id, @RequestBody Board board){
		boardService.๊ธ€์ˆ˜์ •ํ•˜๊ธฐ(id, board);
		return new ResponseDto<Integer>(HttpStatus.OK.value(),1);
	}

 

6. ๊ธ€ ์ˆ˜์ • ๊ตฌํ˜„ ์™„๋ฃŒ

profile

YeaLow

@YeaLow

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!