๋ฉ์ธํ๋ฉด(index.jsp)์์ ์์ธ๋ณด๊ธฐ๋ฅผ ๋๋ฅด๋ฉด ๊ธ ์์ธ๋ณด๊ธฐ ํ์ด์ง์ ๊ฐ ์ ์๋๋ก ๋งํฌ๋ฅผ ๊ฑธ์ด์ค๋ค. <a href="/board/${board.id}" class="btn btn-primary">์์ธ๋ณด๊ธฐ</a>
// ๊ธ ์์ธ๋ณด๊ธฐ
@GetMapping("/board/{id}")
public String findById(@PathVariable int id, Model model) {
model.addAttribute("board", boardService.๊ธ์์ธ๋ณด๊ธฐ(id));
return "board/detail";
}
์ปจํธ๋กค๋ฌ์์๋ model ์จ์ detail.jsp๋ก ๋ณด๋ด๊ธฐ~!
public Board ๊ธ์์ธ๋ณด๊ธฐ(int id) {
return boardRepository.findById(id)
.orElseThrow(()->{
return new IllegalArgumentException("๊ธ ์์ธ๋ณด๊ธฐ ์คํจ : ์์ด๋๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค." + id);
});
}
Service์์๋ ๊ธ ์์ธ๋ณด๊ธฐ ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค.
findById์ ๋ฆฌํด๊ฐ์ด Optional์ด๋๊น ๋๋ค์์ ์ด์ฉํด์ ์์ธ์ฒ๋ฆฌ๊น์ง ํ๋น.
<%@ 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>
<button id="btn-delete" class="btn btn-danger">์ญ์ </button>
<br/><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"%>
detail.jsp๋ ๊ธ ์์ธ๋ณด๊ธฐ ํ์ด์ง ์ด๋ค.
๋์๊ฐ๊ธฐ/์์ /์ญ์ ๋ฒํผ์ ๋ง๋ค๊ณ ,
์ปจํธ๋กค๋ฌ model๋ก๋ถํฐ ๋ฐ์ board๋ก title๊ณผ content๋ฅผ get ํด์ ๋ณด์ฌ์ค๋ค.
๊ธ ์์ฑ ํ ๋ ์คํ๋ง ๋ก๊ณ ์ฌ์ง์ ๋ฃ์ด๋์๋ค.
๊ธ ์์ธ๋ณด๊ธฐ๋ฅผ ํ๋ title๊ณผ content๊ฐ ๋์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
์์ธ๋ณด๊ธฐ ๊ธฐ๋ฅ์ ๊ฐ๋จํ๋น~!