게시글 목록 컨트롤러 추가
'''게시글 목록'''
@GetMapping("/")
public String list(Model model) {
List<BoardDto> boardList = boardService.getBoardlist();
model.addAttribute("boardList", boardList);
return "board/list.html";
}
리스트 목록 서비스 추가
@Transactional
public List<BoardDto> getBoardlist() {
List<BoardEntity> boardEntities = boardRepository.findAll();
List<BoardDto> boardDtoList = new ArrayList<>();
for ( BoardEntity boardEntity : boardEntities) {
BoardDto boardDTO = BoardDto.builder()
.id(boardEntity.getId())
.title(boardEntity.getTitle())
.content(boardEntity.getContent())
.writer(boardEntity.getWriter())
.createdDate(boardEntity.getCreatedDate())
.build();
boardDtoList.add(boardDTO);
}
return boardDtoList;
}
list.html 수정 , css 추가
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" th:href="@{/css/board.css}">
</head>
<body>
<!-- HEADER -->
<div th:insert="common/header.html" id="header"></div>
<a th:href="@{/post}">글쓰기</a>
<table>
<thead>
<tr>
<th class="one wide">번호</th>
<th class="ten wide">글제목</th>
<th class="two wide">작성자</th>
<th class="three wide">작성일</th>
</tr>
</thead>
<tbody>
<!-- CONTENTS !-->
<tr th:each="board : ${boardList}">
<td>
<span th:text="${board.id}"></span>
</td>
<td>
<a th:href="@{'/post/' + ${board.id}}">
<span th:text="${board.title}"></span>
</a>
</td>
<td>
<span th:text="${board.writer}"></span>
</td>
<td>
<span th:text="${#temporals.format(board.createdDate, 'yyyy-MM-dd HH:mm')}"></span>
</td>
</tr>
</tbody>
</table>
<!-- FOOTER -->
<div th:insert="common/footer.html" id="footer"></div>
</body>
</html>
table {
border-collapse: collapse;
}
table, th, td {
border: 1px solid black;
}
게시글 수정 페이지 추가
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:action="@{'/post/edit/' + ${boardDto.id}}" method="post">
<input type="hidden" name="_method" value="put"/>
<input type="hidden" name="id" th:value="${boardDto.id}"/>
제목 : <input type="text" name="title" th:value="${boardDto.title}"> <br>
작성자 : <input type="text" name="writer" th:value="${boardDto.writer}"> <br>
<textarea name="content" th:text="${boardDto.content}"></textarea><br>
<input type="submit" value="수정">
</form>
</body>
</html>
상세조회,수정,삭제 추가 컨트롤러
@GetMapping("/post/{po}")
public String detail(@PathVariable("po") Long po, Model model) {
BoardDto boardDTO = boardService.getPost(po);
model.addAttribute("boardDto", boardDTO);
return "board/detail.html";
}
@GetMapping("/post/edit/{po}")
public String edit(@PathVariable("po") Long po, Model model) {
BoardDto boardDTO = boardService.getPost(po);
model.addAttribute("boardDto", boardDTO);
return "board/update.html";
}
@PutMapping("/post/edit/{po}")
public String update(BoardDto boardDTO) {
boardService.savePost(boardDTO);
return "redirect:/";
}
@DeleteMapping("/post/{po}")
public String delete(@PathVariable("po") Long po) {
boardService.deletePost(po);
return "redirect:/";
}
서비스 코드 추가
@Transactional
public BoardDto getPost(Long id) {
Optional<BoardEntity> boardEntityWrapper = boardRepository.findById(id);
BoardEntity boardEntity = boardEntityWrapper.get();
BoardDto boardDTO = BoardDto.builder()
.id(boardEntity.getId())
.title(boardEntity.getTitle())
.content(boardEntity.getContent())
.writer(boardEntity.getWriter())
.createdDate(boardEntity.getCreatedDate())
.build();
return boardDTO;
}
@Transactional
public Long savePost(BoardDto boardDto) {
return boardRepository.save(boardDto.toEntity()).getId();
}
@Transactional
public void deletePost(Long id) {
boardRepository.deleteById(id);
}
댓글남기기