๊ธ์ฐ๊ธฐ ๊ธฐ๋ฅ์ ๊ตฌํํ๊ธฐ ์ํด saveForm.jsp ๋ผ๋ ๊ธ์ฐ๊ธฐ ํ์ด์ง๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.
๋นจ๊ฐ์ ์๋ฌ
**์๋๊ฒฝ๋ก** : 'ํ์ฌ ์์นํ ๊ณณ์ ๊ธฐ์ค'์ผ๋ก ํด์ '๊ทธ๊ณณ์ ์์น'
- `/` : ๋ฃจํธ -> ๊ฐ์ฅ ์ต์์ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ํ๋ค. (Web root)
- `./` : ํ์ฌ ์์น -> ํ์ผ์ด ํ์ฌ ๋๋ ํ ๋ฆฌ๋ฅผ ์๋ฏธํ๋ค.
- `../` : ํ์ฌ ์์น์ ์๋จ ํด๋ -> ์์ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ํ๋ค.
์ด๋ ๊ฒ ๋ณ๊ฒฝํด์ค๋ค!
< BoardController >
@Controller
public class BoardController {
@GetMapping({"", "/"})
public String index() { // ์ปจํธ๋กค๋ฌ์์ ๋ก๊ทธ์ธ ๋ ์ธ์
์ ์ฐพ๋ ๋ฐฉ์ : @AuthenticationPrincipal PrincipalDetail principal
// /WEB-INF/views/index.jsp
return "index";
}
@GetMapping("/board/saveForm")
public String saveForm() {
return "board/saveForm";
}
}
BoardController์์ ์ฃผ์ ๋งคํ๋ ํด์ฃผ๋ฉด ๋๋ค.
๋ก๊ทธ์ธ ํ ํ ์ธ์ฆ์ด ๋ ์ํ์์ ๋ฉ์ธํ๋ฉด์ ๋ณด๋ฉด ์ผ์ชฝ ์๋จ์ ๊ธ์ฐ๊ธฐ๊ฐ ๋์จ๋ค.
๊ธ์ฐ๊ธฐ์๋ /board/saveForm
์ฃผ์๊ฐ ๋งคํ๋์ด ์๋ค. (index.jsp)
< BoardController >
@Controller
public class BoardController {
@GetMapping({"", "/"})
public String index() { // ์ปจํธ๋กค๋ฌ์์ ๋ก๊ทธ์ธ ๋ ์ธ์
์ ์ฐพ๋ ๋ฐฉ์ : @AuthenticationPrincipal PrincipalDetail principal
// /WEB-INF/views/index.jsp
return "index";
}
@GetMapping("/board/saveForm")
public String saveForm() {
return "board/saveForm";
}
}
๋ฐ๋ผ์ /board/saveForm
์ฃผ์๋ฅผ ์ปจํธ๋กค๋ฌ์์ ์ฐพ์ saveForm ์ด๋ผ๋ ๊ธ์ฐ๊ธฐ ํ์ด์ง(jsp)๋ฅผ ๋ฆฌํดํด์ค๋ค.
< saveForm.jsp >
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="../layout/header.jsp"%>
<div class="container">
<form>
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" placeholder="Enter title" id="title">
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea class="form-control summernote" rows="5" id="content"></textarea>
</div>
</form>
<button id="btn-save" 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"%>
summernote์์ ์ง์ง๊ธฐ๋ก ๊ฐ์ ธ๋ค๊ฐ ๋ง๋ค์๋ค!
๊ธ์ฐ๊ธฐ ํ์ด์ง์์ ์ฌ์ฉ์๊ฐ ์ ๋ชฉ(title)๊ณผ ๋ด์ฉ(content)๋ฅผ ์ ๋ ฅํ๊ณ ๊ธ์ฐ๊ธฐ์๋ฃ ๋ฒํผ(id = "btn-save")์ ๋๋ฅด๋ฉด board.js๊ฐ ์๋ํ๋ค.
< board.js >
let index = {
init: function () {
$("#btn-save").on("click", ()=>{
this.save();
});
},
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",
dataType: "json"
}).done(function (resp) {
// ๊ฒฐ๊ณผ๊ฐ ์ ์์ด๋ฉด done ์คํ
alert("๊ธ์์ฑ์ด ์๋ฃ๋์์ต๋๋ค.");
location.href = "/";
}).fail(function (error) {
// ์คํจํ๋ฉด fail ์คํ
alert("ํ์๊ฐ์
์ด ์คํจํ์์ต๋๋ค.");
alert(JSON.stringify(error));
});
},
}
index.init();
saveForm์์ ๊ฐ์ ธ์จ title๊ณผ content๋ฅผ /api/board
์ฃผ์์ json ๋ฐ์ดํฐ ํ์
์ผ๋ก ๋ณด๋ธ๋ค.
< BoardApiController >
// Data๋ง ๋ฆฌํด
@RestController
public class BoardApiController {
@Autowired
private BoardService boardService;
@PostMapping("/api/board")
public ResponseDto<Integer> save(@RequestBody Board board, @AuthenticationPrincipal PrincipalDetail principal) {
boardService.๊ธ์ฐ๊ธฐ(board, principal.getUser());
return new ResponseDto<Integer>(HttpStatus.OK.value(), 1);
}
}
/api/board
์ฃผ์๊ฐ ๋งคํ๋์ด์๋ ๋ฉ์๋๋ก ์๋ค.
์ฌ๊ธฐ์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ board์๋ title๊ณผ content๊ฐ ๋ค์ด์์ ๊ฒ์ด๋ค.
ํ์ง๋ง board ํ
์ด๋ธ์๋ user์ id๋ ํ์ํ๋ฏ๋ก ๋งค๊ฐ๋ณ์์ @AuthenticationPrincipal PrincipalDetail principal
๋ ์ถ๊ฐํ๋ค.
์ด๊ฒ์ ์ปจํธ๋กค๋ฌ์์ ๋ก๊ทธ์ธ ๋ ์ธ์
์ ์ฐพ๋ ๋ฐฉ์์ด๋ค.
board์ principal์์ ๋ค์ด์๋ User๋ฅผ boardService์ ๊ธ์ฐ๊ธฐ ํจ์๋ก ๋๊ธด๋ค.
< BoardService >
@Service // ์คํ๋ง์ด ์ปดํฌ๋ํธ ์ค์บ์ ํตํด์ Bean์ ๋ฑ๋ก์ ํด์ค. (IoC)
public class BoardService {
@Autowired
private BoardRepository boardRepository;
@Transactional // ์ ์ฒด๊ฐ ์ฑ๊ณตํด์ผ commit, ์คํจํ๋ฉด rollback
public void ๊ธ์ฐ๊ธฐ(Board board, User user) { // title, content
board.setCount(0);
board.setUser(user);
boardRepository.save(board);
}
}
Count๋ ์กฐํ์์ธ๋ฐ ๊ธฐ๋ณธ์ 0์ผ๋ก set ํ๋ค.
User์๋ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ user์ setํ๊ณ ,
๋ ํผ์งํ ๋ฆฌ์ saveํ๋ค.
์ด๋ ๊ฒ DB์ save๊น์ง ์ ์์ ์ผ๋ก ์๋ฃ๋๋ฉด ๋ค์ BoardApiController๋ก ๋์์์ HttpStatus๊ฐ ์ ์์ ์ผ๋ก ๋ฆฌํด๋๋ค.
๊ทธ๋ค์ board.js๋ก ๋์์ ๊ฒฐ๊ณผ๊ฐ ์ ์์ด๋ฏ๋ก done์ด ์คํ๋์ด ๊ธ ์์ฑ์ด ์๋ฃ๋์๋ค๋ ๋ฉ์ธ์ง๊ฐ ๋์์ง๊ณ /
์ฃผ์๋ก ๊ฐ๊ฒ ๋๋ค.
/board/saveForm
์ผ๋ก ๋ค์ด์ค๋ฉด ์ปจํธ๋กค๋ฌ์์ ํด๋น ์ฃผ์๋ฅผ ์ฐพ๊ณ ๋ฆฌํด์ผ๋ก saveForm.jsp ํธ์ถ/api/board
์ json ๋ฐ์ดํฐ๋ก ๋ ๋ฆผ/
๋ก ์ด๋