inblog logo
|
jay0628
    SpringBoot

    [Spring Boot] 14. 스프링부트 상점 v1 (5) 화면 연결 (controller)

    김주희's avatar
    김주희
    Mar 18, 2025
    [Spring Boot] 14. 스프링부트 상점 v1 (5) 화면 연결 (controller)
    Contents
    1. main 화면2. detail 화면 3. 상품등록4. 상품수정

    1. main 화면

    0. 첫 화면에서 보이는 것부터 하나씩 고쳐보자!

    notion image
    notion image
     

    1. 헤더 - 상품목록

    ❗
    localhost:8080 의 첫 화면이 상품 목록이고 헤더의 상품 목록을 클릭하면 첫화면인 상품등록 페이지로 가야한다!
    1. header.mustache 수정하기
      1. 상품목록 a태그의 href uri 수정하기
        1. notion image
    1. StoreController list() 생성
      1. 첫 화면(”/”)으로 들어가면 리스트 목록을 보여줘야 하므로 store 폴더의 list.mustache 파일을 리턴해야 한다.
        1. @GetMapping("/") public String list() { return "store/list"; }

    2. 헤더 - 상품등록

    ❗
    헤더의 상품등록을 클릭하면 상품 등록 화면으로 가야 한다
    1. header.mustache 수정하기
      1. 상품등록 a태그의 href uri 수정하기
        1. notion image
    1. StoreController saveForm() 생성
      1. 상품 등록 화면(”/store/save-form”)으로 들어가면 상품등록 페이지를 보여줘야 하므로 store 폴더의 save-form.mustache 파일을 리턴해야 한다.
        1. @GetMapping("/store/save-form") public String saveForm() { return "store/save-form"; }
     

    3. 헤더 - 구매목록

    ❗
    헤더의 구매목록을 클릭하면 구매목록 화면으로 가야 한다
    1. header.mustache 수정하기
      1. 구매목록 a태그의 href uri 수정하기
        1. notion image
    1. LogController list() 생성
      1. 구매목록이므로 StoreController가 아닌 LogController에 만들어야 한다!
      2. 구매목록(”/log”)으로 들어가면 구매목록 페이지를 보여줘야 하므로 log 폴더의 list.mustache 파일을 리턴해야 한다.
        1. @GetMapping("/log") public String list() { return "log/list"; }
     

    4. 상품목록 - 상세보기

    ❗
    상품목록의 상세보기를 클릭하면 특정 상품의 상세보기 화면으로 가야 한다
    1. store/list.mustache 수정하기
      1. 상세보기 a태그의 href uri를 특정 상품 상세보기 화면으로 가도록 수정한다.
      2. 원래는 {{id}} 이런식이어야 하지만 지금은 바로 눈으로 확인 가능하도록 /store/1, /store/2 이런식으로 해두자
        1. notion image
     
    1. StoreController detail() 생성
      1. 특정 상품의 상세보기(”/store/{id}”)를 클릭하면 상세보기 페이지를 보여줘야 하므로 store 폴더의 detail.mustache 파일을 리턴해야한다.
        1. @GetMapping("/store/{id}") public String detail(@PathVariable("id") int id) { return "store/detail"; }
     

    2. detail 화면

    0. 상세보기 화면에서 보이는 수정화면가기와 삭제를 고쳐보자!

    notion image
    notion image
     

    1. 수정화면가기

    ❗
    특정 상품의 상세보기 페이지에서 수정화면가기 하이퍼링크를 클릭하면 수정화면 페이지로 이동해야 한다!
    1. header.mustache 수정하기
      1. 상품목록 a태그의 href uri 수정하기
        1. notion image
    1. StoreController list() 생성
      1. 첫 화면(”/”)으로 들어가면 리스트 목록을 보여줘야 하므로 store 폴더의 list.mustache 파일을 리턴해야 한다.
        1. @GetMapping("/store/{id}/update-form") public String updateForm(@PathVariable("id") int id) { return "store/update-form"; }
     

    2. 삭제 버튼

    ❗
    특정 상품의 상세보기 페이지에서 삭제를 클릭하면 해당 상품은 삭제되고 구매목록 화면으로 돌아가야 한다!
    1. header.mustache 수정하기
      1. 상품목록 a태그의 href uri 수정하기
        1. notion image
    1. StoreController list() 생성
      1. 첫 화면(”/”)으로 들어가면 리스트 목록을 보여줘야 하므로 store 폴더의 list.mustache 파일을 리턴해야 한다.
        1. @PostMapping("/store/{id}/delete") public String delete(@PathVariable("id") int id) { return "redirect:/"; }
     
     

    3. 상품등록

    0. 상품등록 화면에서 보이는 상품등록 버튼을 고쳐보자!

    notion image
    notion image
     
     
     
    notion image
     
     
     

    4. 상품수정

    0. 상품수정 화면에서 보이는 상품수정 버튼을 고쳐보자!

    notion image
    notion image
     
     
     
     
     
    notion image
     
     
     
    controller 만들면서 detail.mustache 수정하기
    StoreController
    1. updateForm()
    1. delete()
    1. save()
    1. update()
     

    전체 코드

    package com.metacoding.storev1.store; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @Controller // IoC(=HashSet / 제어의 역전)에 뜬다 public class StoreController { @GetMapping("/") public String list() { return "store/list"; } @GetMapping("/store/save-form") public String saveForm() { return "store/save-form"; } @GetMapping("/store/{id}") public String detail(@PathVariable("id") int id) { return "store/detail"; } @GetMapping("/store/{id}/update-form") public String updateForm(@PathVariable("id") int id) { return "store/update-form"; } @PostMapping("/store/{id}/delete") public String delete(@PathVariable("id") int id) { return "redirect:/"; } @PostMapping("/store/save") public String save() { return "redirect:/"; } @PostMapping("/store/{id}/update") public String update(@PathVariable("id") int id) { return "redirect:/store/" + id; } }
    package com.metacoding.storev1.log; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class LogController { @GetMapping("/log") public String list() { return "log/list"; } }
    Share article

    jay0628

    RSS·Powered by Inblog