6.메인 화면WEB JAVA SPRING/PROJECT2023. 9. 12. 17:01
Table of Contents
1.MainItemDto/**
* 메인 페이지에서 상품을 보여줄때 사용
*/
package com.shop.dto;
import com.querydsl.core.annotations.QueryProjection;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class MainItemDto {
private Long id;
private String itemNm;
private String itemDetail;
private String imgUrl;
private Integer price;
@QueryProjection //Querydsl 결과 조회시 MainItemDto 객체로 바로 받아옴
public MainItemDto(Long id, String itemNm, String itemDetail, String imgUrl,Integer price){
this.id = id;
this.itemNm = itemNm;
this.itemDetail = itemDetail;
this.imgUrl = imgUrl;
this.price = price;
}
}
2.ItemRepositoryCustom//메인페이지 상품 리스트 추가
Page<MainItemDto> getMainItemPage(ItemSearchDto itemSearchDto, Pageable pageable);
3.ItemRepositoryCustomImpl
//검색어가 null이 아니면 상품명에 해당 검색어가 포함되는 상품을 조회하는 조건 반환
private BooleanExpression itemNmLike(String searchQuery) {
return StringUtils.isEmpty(searchQuery) ? null : QItem.item.itemNm.like("%" + searchQuery + "%");
}
@Override
public Page<MainItemDto> getMainItemPage(ItemSearchDto itemSearchDto, Pageable pageable) {
QItem item = QItem.item;
QItemImg itemImg = QItemImg.itemImg;
QueryResults<MainItemDto> results = queryFactory
.select(
new QMainItemDto( //QMainItemDto의 생성자에 반환할 값 넣어줌
item.id,
item.itemNm,
item.itemDetail,
itemImg.imgUrl,
item.price)
)
.from(itemImg)
.join(itemImg.item, item) //itemImg과 item을 내부조인
.where(itemImg.repimgYn.eq("Y")) //대표 상품 이미지만 불러옴
.where(itemNmLike(itemSearchDto.getSearchQuery()))
.orderBy(item.id.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetchResults();
List<MainItemDto> content = results.getResults();
long total = results.getTotal();
return new PageImpl(content, pageable, total);
}
4.ItemService
//메인 페이지 보여줄 상품 데이터 조회
@Transactional(readOnly = true)
public Page<MainItemDto> getMainItemPage(ItemSearchDto itemSearchDto, Pageable pageable){
return itemRepository.getMainItemPage(itemSearchDto, pageable);
}
5.MainController
//메인 페이지 화면단 뿌림
package com.shop.controller;
import com.shop.dto.ItemSearchDto;
import com.shop.dto.MainItemDto;
import com.shop.service.ItemService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Optional;
@Controller
@RequiredArgsConstructor
public class MainController {
private final ItemService itemService;
@GetMapping(value = "/")
public String main(ItemSearchDto itemSearchDto, Optional<Integer> page, Model model){
Pageable pageable = PageRequest.of(page.isPresent() ? page.get() : 0, 6);
Page<MainItemDto> items = itemService.getMainItemPage(itemSearchDto, pageable);
model.addAttribute("items", items);
model.addAttribute("itemSearchDto", itemSearchDto);
model.addAttribute("maxPage", 5);
return "main";
}
}
main.html
<div layout:fragment="content">
<!--그림슬라이드 출처 : 부트스트랩-->
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
<li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
</ol>
<div class="carousel-inner">
<div class="carousel-item active">
<img class="d-block w-100" src="https://img.freepik.com/free-vector/flat-science-twitch-banner_23-2149069947.jpg?w=2000" alt="First slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://img.freepik.com/free-vector/flat-science-twitch-banner_23-2149069947.jpg?w=2000" alt="Second slide">
</div>
<div class="carousel-item">
<img class="d-block w-100" src="https://img.freepik.com/free-vector/flat-science-twitch-banner_23-2149069947.jpg?w=2000" alt="Third slide">
</div>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<input type="hidden" name="searchQuery" th:value="${itemSearchDto.searchQuery}">
<div th:if="${not #strings.isEmpty(itemSearchDto.searchQuery)}" class="center">
<p class="h3 font-weight-bold" th:text="${itemSearchDto.searchQuery} + '검색 결과'"></p>
</div>
<div class="row">
<th:block th:each="item, status: ${items.getContent()}">
<div class="col-md-4 margin">
<div class="card">
<a th:href="'/item/' +${item.id}" class="text-dark">
<img th:src="${item.imgUrl}" class="card-img-top" th:alt="${item.itemNm}" height="400">
<div class="card-body">
<h4 class="card-title">[[${item.itemNm}]]</h4>
<p class="card-text">[[${item.itemDetail}]]</p>
<h3 class="card-title text-danger">[[${item.price}]]원</h3>
</div>
</a>
</div>
</div>
</th:block>
</div>
<div th:with="start=${(items.number/maxPage)*maxPage + 1}, end=(${(items.totalPages == 0) ? 1 : (start + (maxPage - 1) < items.totalPages ? start + (maxPage - 1) : items.totalPages)})" >
<ul class="pagination justify-content-center">
<li class="page-item" th:classappend="${items.number eq 0}?'disabled':''">
<a th:href="@{'/' + '?searchQuery=' + ${itemSearchDto.searchQuery} + '&page=' + ${items.number-1}}" aria-label='Previous' class="page-link">
<span aria-hidden='true'>Previous</span>
</a>
</li>
<li class="page-item" th:each="page: ${#numbers.sequence(start, end)}" th:classappend="${items.number eq page-1}?'active':''">
<a th:href="@{'/' +'?searchQuery=' + ${itemSearchDto.searchQuery} + '&page=' + ${page-1}}" th:inline="text" class="page-link">[[${page}]]</a>
</li>
<li class="page-item" th:classappend="${items.number+1 ge items.totalPages}?'disabled':''">
<a th:href="@{'/' +'?searchQuery=' + ${itemSearchDto.searchQuery} + '&page=' + ${items.number+1}}" aria-label='Next' class="page-link">
<span aria-hidden='true'>Next</span>
</a>
</li>
</ul>
</div>
</div>
'WEB JAVA SPRING > PROJECT' 카테고리의 다른 글
8.주문 이력 조회 (0) | 2023.09.12 |
---|---|
7.주문 기능 (0) | 2023.09.12 |
5.상품 관리하기 : Querydsl (0) | 2023.09.12 |
4.상품 수정하기 (0) | 2023.09.12 |
3.상품 등록하기 (0) | 2023.09.12 |