62. 상세페이지 이전글, 다음글
-이전글, 다음글
*저장소 수정(BoardRepository.java)
package com.example.repository;
import java.util.List;
import com.example.entity.Board;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface BoardRepository extends JpaRepository<Board, Long>{
//검색 조건(제목 또는 작성자에 해당 내용이 있는지)
List<Board> findByTitleOrWriterContainingOrderByNoDesc(
String title, String writer, Pageable pageable);
Long countByTitleOrWriterContaining(String title, String writer);
//이전글(큰 것들 중 가장 작은 것)
Board findTop1ByNoLessThanOrderByNoDesc(long no);
//다음글(작은 것들 중 가장 큰 것)
Board findTop1ByNoGreaterThanOrderByNoAsc(long no);
}
* /select_one get 수정(BoardController.java)
@GetMapping(value="/select_one")
public String selecOneGET(Model model,
@RequestParam("no") long no) {
Optional<Board> board = bRepository.findById(no);
model.addAttribute("board", board.orElse(null));
//이전글
Board prev = bRepository.findTop1ByNoLessThanOrderByNoDesc(no);
model.addAttribute("prev", 0);
if(prev != null) {
model.addAttribute("prev", prev.getNo());
}
//다음글
Board next = bRepository.findTop1ByNoGreaterThanOrderByNoAsc(no);
model.addAttribute("next", 0);
if(next != null) {
model.addAttribute("next", next.getNo());
}
return "board_select_one";
}
* 이전글, 다음글 태그 추가(board_select_one.jsp)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>상세 페이지</title>
</head>
<body>
<h3>상세 페이지</h3>
<hr/>
글번호 : <div style="display: inline-block;" th:text="${board.no}"></div><br/>
글제목 : <div style="display: inline-block" th:text="${board.title}"></div><br />
글내용 : <div style="display: inline-block" th:text="${board.content}"></div><br />
작성자 : <div style="display: inline-block" th:text="${board.writer}"></div><br />
조회수 : <div style="display: inline-block" th:text="${board.hit}"></div><br />
날짜 : <div style="display: inline-block" th:text="${board.regdate}"></div><br />
이미지 : <img th:src="@{/board/select_image(no=${board.no})}" style="width: 50px; height: 50px;"></div><br />
<hr/>
<a th:href="@{/board/select}">목록으로</a>
<a th:if="${prev != 0}" th:href="@{/board/select_one(no=${prev})}">이전글</a>
<a th:if="${next != 0}" th:href="@{/board/select_one(no=${next})}">다음글</a>
</body>
</html>
* 화면 확인
* 마지막, 처음 글에서는 각각 다음글, 이전글이 없으므로 나타나지 않는다.
'Spring' 카테고리의 다른 글
Spring 일지 #64 (exam) 시험 대비9(수정하기) (0) | 2021.10.08 |
---|---|
Spring 일지 #63 (exam) 시험 대비8(삭제하기) (0) | 2021.10.08 |
Spring 일지 #61 (exam) 시험 대비6(상세 페이지, 이미지 불러오기) (0) | 2021.10.07 |
Spring 일지 #60 (exam) 시험 대비5(검색창, 조회수 증가) (0) | 2021.10.07 |
Spring 일지 #59 (exam) 시험 대비4(게시판 목록, 페이지네이션) (0) | 2021.10.07 |