Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- 스프링이미지업로드
- 파이썬sort
- springboot_exception_handler
- centos도커설치
- 출처 코딩셰프
- 출처 문어박사
- 스프링구독
- 도커설치하는법
- WAS웹서버
- 스프링익셉션처리
- 인스타클론
- 스프링사진업로드
- 스프링부트api
- 스프링사진
- dockerinstall
- 출처 메타코딩
- 서버에도커설치
- vm도커설치하는법
- 출처 따배도
- 스프링부트서버에사진전송
- ssh도커설치
- 우분투도커설치
- 스프링부트구독취소
- 스프링부트팔로우취소
- 스프링부트중복예외처리
- 스프링부트
- 출처 노마드코더
- 스프링부트팔로잉
- 스프링부트사진올리기
- 멀티폼
Archives
- Today
- Total
MakerHyeon
[springBoot] 스토리 -스토리 페이징 로딩 구현 본문
스토리 -스토리 페이징 로딩 구현
1. Story뷰 렌더링
- 스토리 데이터가 들어갈 class 생성
// story.jsp
<main class="main">
<section class="container">
<!--전체 리스트 시작-->
<article class="story-list" id="storyList">
<!--전체 리스트 아이템-->
</article>
</section>
- Ajax로 데이터 뿌리기
function storyLoad() {
$.ajax({
url:`/api/image?page=${page}`,
dataType:"json",
}).done(res=>{
res.data.content.forEach((image)=>{
let storyItem = getStoryItem(image);
$("#storyList").append(storyItem);
})
}).fail(error=>{
})
}
storyLoad();
function getStoryItem(image) {
let item = `<div class="story-list__item">
<div class="sl__item__header">
<div>
<img class="profile-image" src="/upload/${image.user.profileImageUrl}"
onerror="this.src='/images/person.jpeg'" />
</div>
<div>${image.user.username}</div>
</div>
<div class="sl__item__img">
<img src="/upload/${image.postImageUrl}" />
</div>
<div class="sl__item__contents">
<div class="sl__item__contents__icon">
<button>
<i class="fas fa-heart active" id="storyLikeIcon-1" onclick="toggleLike()"></i>
</button>
</div>
<span class="like"><b id="storyLikeCount-1">3 </b>likes</span>
<div class="sl__item__contents__content">
<p>${image.caption}</p>
</div>
<div id="storyCommentList-1">
<div class="sl__item__contents__comment" id="storyCommentItem-1"">
<p>
<b>Lovely :</b> 부럽습니다.
</p>
<button>
<i class="fas fa-times"></i>
</button>
</div>
</div>
<div class="sl__item__input">
<input type="text" placeholder="댓글 달기..." id="storyCommentInput-1" />
<button type="button" onClick="addComment()">게시</button>
</div>
</div>
</div>`;
return item;
}
2. 페이지 구현
- 컨트롤러의 스토리함수에 파라미터 생성
- 리턴값 Page로 바꾸기
- 서비스에 페이지 파라미터 전달
// ImageApiController.java
import org.springframework.data.domain.Pageable;
@GetMapping("/api/image")
public ResponseEntity<?> imageStory(@AuthenticationPrincipal PrincipalDetails principalDetails,
@PageableDefault(size=3) Pageable pageable){
Page<Image> images = imageService.이미지스토리(principalDetails.getUser().getId(),pageable);
return new ResponseEntity<>(new CMRespDto<>(1,"성공",images), HttpStatus.OK);
}
- 리포지터리에 페이지 파라미터 전달
// ImageService.java
@Transactional(readOnly = true)
public Page<Image> 이미지스토리(int principalId, Pageable pageable){
Page<Image> images = imageRepository.mStory(principalId,pageable);
return images;
}
-리포지터리에서 인자받기 및 쿼리로 ORDER BY (NativeQuery라서)
// ImageRepository.java
@Query(value="SELECT * FROM image WHERE userId IN
(SELECT toUserId FROM subscribe WHERE fromuserId=:principalId)
ORDER BY id DESC;",nativeQuery=true)
Page<Image> mStory(int principalId, Pageable pageable);
3. 스크롤 페이징 로딩 구현
// story.js
...
// (1) 스토리 로드하기
let page = 0;
function storyLoad() {
$.ajax({
url:`/api/image?page=${page}`,
dataType:"json",
...
// (2) 스토리 스크롤 페이징하기
$(window).scroll(() => {
//console.log("윈도우 scrollTop",$(window).scrollTop());
//console.log("문서의 높이",$(document).height());
//console.log("윈도우 높이",$(window).height);
let checkNum = $(window).scrollTop() - ($(document).height - $(window).height());
if(checkNum<1 && checkNum > -1){
page++;
storyLoad();
}
});
'SpringBoot' 카테고리의 다른 글
RestController-JSON반환 (0) | 2023.02.23 |
---|---|
[SpringBoot] Jwt token을 사용해보자. (실습포함) (0) | 2023.01.26 |
[springBoot] 스토리 -스토리 API (0) | 2023.01.13 |
[springBoot] 구독정보 -뷰 렌더링/구독,구독취소기능구현 (0) | 2023.01.12 |
[springBoot] 구독정보 -join / scalar subquery (feat.qlrm) (0) | 2023.01.12 |
Comments