728x90
SpringBoot · intelliJ · OracleDB · Thymeleaf
답글페이징(댓글) 을 만들어보자
1. AnswerRepository 추가
package com.gosari.repick_project.answer;
import com.gosari.repick_project.answer.Answer;
import com.gosari.repick_project.question.Question;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AnswerRepository extends JpaRepository <Answer, Integer>{
Page<Answer> findAllByQuestion(Question question, Pageable pageable);
}
2.AnswerService 추가
/*페이징*/
public Page<Answer> getList(Question question, int page){
List<Sort.Order> sorts = new ArrayList<>();
sorts.add(Sort.Order.desc("createDate"));
Pageable pageable = PageRequest.of(page, 5);
return this.answerRepository.findAllByQuestion(question, pageable);
}
3.QuestionController 수정
/*질문상세*/ //댓글페이징추가
@RequestMapping(value = "/detail/{id}")
public String detail(Model model, @PathVariable("id") Integer id, AnswerForm answerForm,
@RequestParam(value="page", defaultValue="0") int page) {
/*댓글페이징*/
Question question = this.questionService.getQuestion(id);
Page<Answer> paging = this.answerService.getList(question, page);
model.addAttribute("paging", paging);
model.addAttribute("question", question);
/*이전글다음글번호와 제목을 html에서 불러올수있게 model.addAttribute() 작성*/
QuestionPage questionPage = questionPageRepository.findByPages(id);
model.addAttribute("prevID", questionPage.getPREVID());
model.addAttribute("prevSub", questionPage.getPREV_SUB());
model.addAttribute("nextID", questionPage.getNEXTID());
model.addAttribute("nextSub", questionPage.getNEXT_SUB());
return "question_detail";
}
Question question = this.questionService.getQuestion(id);
을 상단으로 옮기고,
Page<Answer> paging = this.answerService.getList(question, page);
model.addAttribute("paging", paging);
model.addAttribute("question", question);
을 추가해준다.
2.question_detail.html 페이징 추가
~생략~
<!--답변삭제버튼-->
<a href="javascript:void(0);" th:data-uri="@{|/answer/delete/${answer.id}|}"
class="delete btn btn-sm btn-outline-secondary" sec:authorize="isAuthenticated()"
th:if="${answer.author != null and #authentication.getPrincipal().getUsername() == answer.author.username}"
th:text="삭제"></a>
</div>
</div>
</div>
<!--답변 반복 끝-->
<!-- 답변 페이징처리 시작 -->
<div th:if="${!paging.isEmpty()}">
<ul class="pagination justify-content-center">
<li class="page-item" th:classappend="${!paging.hasPrevious} ? 'disabled'">
<a class="page-link"
th:href="@{|?page=${paging.number-1}|}">
<span>이전</span>
</a>
</li>
<li th:each="page: ${#numbers.sequence(0, paging.totalPages-1)}"
th:if="${page >= paging.number-5 and page <= paging.number+5}"
th:classappend="${page == paging.number} ? 'active'"
class="page-item">
<a th:text="${page}" class="page-link" th:href="@{|?page=${page}|}"></a>
</li>
<li class="page-item" th:classappend="${!paging.hasNext} ? 'disabled'">
<a class="page-link" th:href="@{|?page=${paging.number+1}|}">
<span>다음</span>
</a>
</li>
</ul>
</div>
<!-- 페이징처리 끝 -->
<!--답변 작성-->
<form th:action="@{|/answer/create/${question.id}|}"
th:object="${answerForm}" method="post" class="my-3">
~생략~
댓글 앵커 수정 필요
728x90
'Follow Work > SpringbootBoard' 카테고리의 다른 글
[API] 네이버지도 API 추가 [34] (0) | 2022.09.05 |
---|---|
[Springboot] 유저 회원가입 [33] (0) | 2022.08.25 |
[Springboot] 닉네임 만들기 [31] (0) | 2022.08.25 |
[Springboot] 이전글 다음글 만들기 [30] (0) | 2022.08.25 |
[StringBoot] 카테고리 만들기 [29] (0) | 2022.08.24 |