개인프로젝트-03

이동원·2024년 7월 4일
  • 카테고리를 눌렀을때 ,상세보기 페이지로 넘어가지않고 지금 화면 그대로에서 해당 카테고리인 상품리스트만 나오게 리팩토링해보자
<!--product_list-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>리스트</title><!-- 위에 페이지 이름 지정해줌 -->

</head>

<body>
<h2>카테고리 리스트</h2>
<ul>
    <li th:each="category : ${categoryList}">
        <a th:href="@{|/category/${category.id}|}">
            <span th:text="${category.name}"></span>
        </a>
    </li>
</ul>


<div><a th:href="@{/category/create}">카테고리 생성</a></div>
<div><a th:href="@{/category/delete}">카테고리 삭제</a></div>

<h2>상품 리스트</h2>

<ul>
    <li th:each="product : ${productList}">
        <a th:href="@{|/product/${product.id}|}"> <!--  경로에 변수 넣을때는 @{||}-->
        <span th:text="${product.name}"></span>
        <span th:text="${product.price}"></span>
        </a>
    </li>
</ul>
<div><a th:href="@{/product/create}">상품 생성</a></div>
<div><a th:href="@{/product/delete}">상품 삭제</a></div>


</body>
</html>

    //카테고리 아이디로 상세보기
    @GetMapping("/{id}")  // @PathVariable  이름하고 같아야한다 .
    public String getCategoryById(@PathVariable("id") Long categoryId, Model model) {
        List<Category> categoryList = categoryService.getCategoryList();
        Category category = categoryService.getCategoryById(categoryId);
        // 카테고리의 제품 목록을 모델에 추가 (가정)
        List<Product> productList = productService.getProductByCategory(category);
        model.addAttribute("productList", productList);
        model.addAttribute("categoryList",categoryList);
        return "product_list";
    }

0개의 댓글