Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- springboot 미니프로젝트
- 스프링부트 게시판만들기
- 유니티Cube
- 유니티기초
- springboot 게시판 프로젝트
- 타입스크립트 기본문법
- 파이썬 기초
- springboot 게시판만들기
- 스프링부트 블로그만들기
- 스프링부트 회원가입
- 타입스크립트 기초
- 유니티
- 스프링부트 블로그
- 스프링게시판프로젝트
- python 괄호 회전하기
- 타입스크립트 기초문법
- 스프링부트 update
- spring jpa 게시판
- JS기초
- spring jpa 사이드프로젝트
- 스프링부트 미니프로젝트
- 파이썬 괄호 회전하기
- typescript 기초문법
- 괄호 회전하기 파이썬
- jpa 게시판
- 프로그래머스 괄호 회전하기 python
- 스프링 게시판 만들기
- springboot 사이드프로젝트
- springboot 게시판
- 유니티Material
Archives
- Today
- Total
Digking's cave
My First Blog Project (18) : 게시판 글 상세보기 / 삭제하기 / 수정하기 본문
Spring/My First Blog Project
My First Blog Project (18) : 게시판 글 상세보기 / 삭제하기 / 수정하기
디깅 2022. 12. 26. 11:23728x90
BoardCotnroller.java
package com.cos.blog.controller;
import com.cos.blog.config.auth.PrincipalDetail;
import com.cos.blog.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class BoardController {
@Autowired
private BoardService boardService;
@GetMapping({"","/"})
public String index(Model model,@PageableDefault(size = 3,sort = "id",direction = Sort.Direction.DESC) Pageable pageable){
model.addAttribute("boards",boardService.글목록(pageable));
return "index"; //viewResolver 작동
}
@GetMapping("/board/{id}")
public String findById(@PathVariable int id,Model model){
model.addAttribute("board",boardService.글상세보기(id));
return "board/detail";
}
@GetMapping("board/{id}/updateForm")
public String updateForm(@PathVariable int id, Model model){
model.addAttribute("board",boardService.글상세보기(id));
return "board/updateForm";
}
//USER권한이 필요
@GetMapping("/board/saveForm")
public String saveForm(){
return "board/saveForm";
}
}
findById() 생성
BoardService.java
package com.cos.blog.service;
import com.cos.blog.model.Board;
import com.cos.blog.model.User;
import com.cos.blog.repository.BoardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class BoardService {
@Autowired
private BoardRepository boardRepository;
@Transactional
public void 글쓰기(Board board, User user){
board.setCount(0);
board.setUser(user);
boardRepository.save(board);
}
@Transactional(readOnly = true)
public Page<Board> 글목록(Pageable pageable) {
return boardRepository.findAll(pageable);
}
@Transactional(readOnly = true)
public Board 글상세보기(int id) {
return boardRepository.findById(id)
.orElseThrow(()->{
return new IllegalArgumentException("글 상세보기 실패: 아이디를 찾을 수 없습니다.");
});
}
@Transactional
public void 글삭제하기(int id) {
boardRepository.deleteById(id);
}
@Transactional
public void 글수정하기(int id, Board requestBoard) {
// 영속화를 먼저 시킨다
Board board = boardRepository.findById(id)
.orElseThrow(()->{
return new IllegalArgumentException("글 찾기 실패: 아이디를 찾을 수 없습니다.");
}); //영속화 완료
board.setTitle(requestBoard.getTitle());
board.setContent(requestBoard.getContent());
//해당 함수로 종료시(Service가 종료될 때) 트랜잭션이 종료된다. 이때 더티체킹이 일어나서 자동업데이트가 된다(db flush)
}
}
글상세보기 service 생성
글 수정하기는 기존의 글을 가져와서 수정해야한다.
먼저 영속화를 시킨 후, 그 값에 set을 통해 값을 udpate한다.
@Transactional 을 활용해서, update 되면 더티체킹으로 자동으로 DB flush를 통해 업데이트가 진행된다.
WEB-INF/views/board/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>
<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/><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" %>
수정하기는 새로운 form으로 이동하기 때문에 a href를 활용한다.
삭제하기는 js로 연결
board.js
let index = {
init: function(){
$("#btn-save").on("click",()=>{
this.save();
});
$("#btn-delete").on("click",()=>{
this.deleteById();
});
$("#btn-update").on("click",()=>{
this.update();
});
},
save:function(){
let data = {
title: $("#title").val(),
content: $("#content").val()
};
$.ajax({
type:"POST",
url:"/api/board",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8"
}).done(function(resp){
alert("글쓰기가 완료되었습니다.");
location.href = "/";
}).fail(function(error){
alert(JSON.stringify(error));
});
},
deleteById:function(){
let id = $("#id").text();
$.ajax({
type:"DELETE",
url:"/api/board/"+id,
}).done(function(resp){
alert("삭제가 완료되었습니다.");
location.href = "/";
}).fail(function(error){
alert(JSON.stringify(error));
});
},
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"
}).done(function(resp){
alert("글수정이 완료되었습니다.");
location.href = "/";
}).fail(function(error){
alert(JSON.stringify(error));
});
},
}
index.init();
반응형
'Spring > My First Blog Project' 카테고리의 다른 글
My First Blog Project (19) : 블로그 회원정보 수정 (0) | 2022.12.26 |
---|---|
My First Blog Project (17) : 게시판 글 목록 페이징처리 (0) | 2022.12.26 |
My First Blog Project (16) : 게시판 글 목록 보기 (0) | 2022.12.26 |
My First Blog Project (15) : 게시판 글쓰기 (0) | 2022.12.21 |
My First Blog Project (14) : SpringSecurity 로그인 구현 (0) | 2022.12.21 |