Spring/GuestBook
GuestBook #3 페이징 처리가 되어있는 목록보기부터 검색까지
java나유
2022. 10. 29. 18:19
3)상세 보기
⇒페이징 처리가 되어있는 경우 상세보기로 이동할 때는 기본키의 값과 현재 페이지 번호를 같이 넘겨 주는 것이 좋다. 상세보기를 하다가 목록보기를 눌렀을 때 첫번째 페이지로 이동하는 것 보다는 상세보기를 수행했던 페이지로 이동하는 것이 UI 측면에서 옳은 방향
⇒list.html파일에 제목부분을 출력하는 곳에 상세보기 링크 추가
<td><a th:href="@{/guestbook/read(gno=${dto.gno}, page=${result.page})}">[[${dto.title}]]</a></td>
⇒Service 인터페이스에 상세보기 처리를 위한 메서드 선언
public GuestBookDTO read(Long gno);
⇒ServiceImpl 클래스에 상세보기 처리 메서드를 구현
//상세페이지
@Override
public GuestBookDTO read(Long gno) {
Optional<GuestBook> guestBook =
guestBookRepository.findById(gno);
return guestBook.isPresent()? entityToDTO(guestBook.get()) : null;
}
=>Controller 클래스에 상세보기 처리 메서드를 구현
//상세보기 요청 처리
@GetMapping("/guestbook/read")
//ModelAttribute는 매개변수를 결과 페이지에 넘겨줄 때 사용
public void read(long gno,
@ModelAttribute("requestDTO") PageRequestDTO requestDTO,
Model model){
GuestBookDTO dto = guestBookService.read(gno);
model.addAttribute("dto", dto);
}
⇒templates/guestbook 디렉토리에 read.html파일을 생성해서 데이터 출력
<!DOCTYPE html>
<html lang="en"
xmlns:th="http://www.thymeleaf.org">
<th:block th:replace="~{/layout/basic:: setContent(~{this::content})}">
<th:block th:fragment="content">
<h1>방명록 상세보기</h1>
<div class="form-group">
<label>글번호</label>
<input type="text" class="form-control"
name="gno" th:value="${dto.gno}"
readonly />
</div>
<div class="form-group">
<label>제목</label>
<input type="text" class="form-control"
name="title" th:value="${dto.title}"
readonly />
</div>
<div class="form-group">
<label>내용</label>
<textarea class="form-control" rows="5"
name="content" readonly>[[${dto.content}]]</textarea>
</div>
<div class="form-group">
<label>작성자</label>
<input type="text" class="form-control"
name="writer" th:value="${dto.writer}"
readonly />
</div>
<div class="form-group">
<label>작성일</label>
<input type="text" class="form-control"
name="regDate"
th:value="${#temporals.format(dto.regDate, 'yyyy/MM/dd HH:mm:ss')}"
readonly />
</div>
<div class="form-group">
<label>수정일</label>
<input type="text" class="form-control"
name="modDate"
th:value="${#temporals.format(dto.modDate, 'yyyy/MM/dd HH:mm:ss')}"
readonly />
</div>
<a th:href="@{/guestbook/link(page=${requestDTO.page})}">
<button type="button" class="btn btn-info">
목록보기
</button>
</a>
</th:block>
</th:block>
4)검색 구현
⇒PageRequestDTO 클래스에 검색 타입과 키워드를 저장하기 위한 속성을 추가
PageRequestDTO
//검색타입
private String type;
//검색어
private String keyword;
⇒ServiceImpl클래스에 검색 조건을 만드는 메서드 추가
GuestBookServiceImpl
public BooleanBuilder getSearch(PageRequestDTO requestDTO){
String type=requestDTO.getType();
String keyword=requestDTO.getKeyword();
BooleanBuilder booleanBuilder =
new BooleanBuilder();
QGuestBook qGuestBook = QGuestBook.guestBook;
//검색 조건이 없는 경우
if(type==null || type.trim().length()==0){
return booleanBuilder;
}
//검색 조건이 있는 경우
BooleanBuilder conditionBuilder=new BooleanBuilder();
if(type.contains("t")){
conditionBuilder.or(
qGuestBook.title.contains(keyword));
}
if(type.contains("c")){
conditionBuilder.or(
qGuestBook.content.contains(keyword));
}
if(type.contains("w")){
conditionBuilder.or(
qGuestBook.writer.contains(keyword));
}
booleanBuilder.and(conditionBuilder);
return booleanBuilder;
}
=>ServiceImpl 클래스의 목록을 가져오는 메서드 수정
@Override
public PageResponseDTO<GuestBookDTO, GuestBook> getList(PageRequestDTO requestDTO) {
//페이지 단위 요청을 위한 Pageable 객체를 생성
Pageable pageable = requestDTO.getPageable(
Sort.by("gno").descending());
//데이터베이스에서 조회
//Page<GuestBook> result = guestBookRepository.findAll(pageable);
BooleanBuilder booleanBuilder =
getSearch(requestDTO);
Page<GuestBook> result =
guestBookRepository.findAll(
booleanBuilder, pageable);
//Entity를 DTO로 변환하기 위한 객체 생성
Function<GuestBook, GuestBookDTO> fn =
(entity -> entityToDTO(entity));
//데이터 목록 생성
return new PageResponseDTO<>(result, fn);
}
728x90