Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[WebPage] RedirectAttributes 본문

인프런/[인프런] 스프링 MVC 1

[WebPage] RedirectAttributes

Seung__ 2021. 9. 17. 21:29
  • 상품을 저장하고 Redirect로 해당 상품의 상세 화면으로 넘어갔다.
  • 근데 저장이 된건지..뭔지..뭔가 표시가 없다.
  • 정상적으로 처리가 되었으면 메시지를 보여줬으면 좋겠따.
  • RedirectAttributes를 사용하면 간단히 해결 가능.

 

 

1. RedirectAttributes 적용.


  • RedirectAttributes에 넣은 attributes는 PathVariable, QueryParam으로 사용이 가능.
  • 아래의 경우 "itemId"라는 이름으로 id값을 RedirectAttributes에 추가했고 이는 PathVariable로 사용 가능.
  • PathVariable로 사용되지 않은 attributes는 queryParameter로 뒤에 붙음.
  • redirect:/basic/items/{itemId}?status=true 
  • +인코딩도 해결.
    @PostMapping("/add")
    public String addItemV6(Item item, RedirectAttributes redirectAttributes){
        Item savedItem = itemRepository.save(item);
        redirectAttributes.addAttribute("itemId", savedItem.getId());
        redirectAttributes.addAttribute("status", true);
        return "redirect:/basic/items/{itemId}" ; //나머지 attributes는 queryParameter로 붙음.
    }

오 반영됨.

 

 

2. item.html에서 RedirectAttributes로 넘어온 값 사용.


  • status값은 queryParam으로 넘어온다.
  • status값은 param.status로 꺼낼 수 있다.
    • ${param.~} : Thymeleaf에서 queryParam을 쉽게 사용하도록 제공하는 기능.
  • staus가 true이면 '저장 완료'를 화면에 추가함.
    <h2 th:if="${param.status}" th:text="'저장 완료'"></h2>

 

 

3.GitHub : 210917 RedirectAttributes


 

GitHub - bsh6463/MVC3

Contribute to bsh6463/MVC3 development by creating an account on GitHub.

github.com

 

'인프런 > [인프런] 스프링 MVC 1' 카테고리의 다른 글

[WebPage] PRG Post/Redirect/Get  (0) 2021.09.17
[WebPage] 상품 수정  (0) 2021.09.17
[WebPage] 상품등록 - @ModelAttribute  (0) 2021.09.17
[WebPage] 상품 등록 form  (0) 2021.09.17
[WebPage] 상품 상세  (0) 2021.09.17
Comments