๊ด€๋ฆฌ ๋ฉ”๋‰ด

Tech Log ๐Ÿ› ๏ธ

PRG๋ž€? (Redirect๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ ) ๋ณธ๋ฌธ

์Šคํ”„๋ง mvc

PRG๋ž€? (Redirect๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ )

sehaan 2022. 3. 20. 18:59

"Redirect๋ฅผ ์™œ ํ•„์š”ํ• ๊นŒ?"

 

PRG๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์ „์— ๋จผ์ € Rediect์˜ ํ•„์š”์„ฑ์„ ์•Œ ํ•„์š”๊ฐ€ ์žˆ๋‹ค.

 

๋งŒ์•ฝ ์ƒํ’ˆ์„ ์ˆ˜์ •, ๊ฐœ๋ฐœํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐœ๋ฐœํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•ด๋ณด์ž 

 

@PostMapping("/addForm")
public String addCafe(@ModelAttribute Cafe cafe){
    cafeRepository.save(cafe);
    return "basicCafe/cafe";
}

์นดํŽ˜ ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ถ”๊ฐ€๋ฅผ ์™„๋ฃŒํ•œ ๋’ค ์ƒˆ๋กœ ๊ณ ์นจ์„ ๋ˆ„๋ฅด๊ฒŒ ๋œ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

 

๋˜‘๊ฐ™์€ ์ •๋ณด๋“ค์ด ๊ณ„์†ํ•ด์„œ ์ž…๋ ฅ๋  ๊ฒƒ์ด๋‹ค.

์ด๋Š” Redirect๋ฅผ ์„ค์ •ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์ธ๋ฐ, ์ด๋ ‡๊ฒŒ๋˜๋ฉด ๋งˆ์ง€๋ง‰์œผ๋กœ ์„œ๋ฒ„์— ์ „์†กํ•œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ„์† ์‹คํ–‰์‹œํ‚จ๋‹ค.

POST, Redirect Get

 

PRG๋Š” ์ƒˆ๋กœ๊ณ ์นจ ์‹œ ๋˜‘๊ฐ™์€ ๋ฉ”์„œ๋“œ๊ฐ€ ๊ณ„์†ํ•ด์„œ ์‹คํ–‰๋˜๋Š” ๊ฒƒ์„ ๋ง‰์•„์ค€๋‹ค.

๋ง ๊ทธ๋Œ€๋กœ ๊ฒฝ๋กœ๋ฅผ ์šฐํšŒํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. 

๋ฉ”์„œ๋“œ ์‹คํ–‰ ์™„๋ฃŒ ํ›„ ๋‹ค๋ฅธ ๊ฒฝ๋กœ๋กœ ํ˜ธ์ถœํ•ด์คŒ์œผ๋กœ์จ ์„œ๋ฒ„๋Š” ๋” ์ด์ƒ ๋˜‘๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š๊ฒŒ ๋œ๋‹ค!

 

 

๊ทธ๋Ÿผ ์ด์ œ ์œ„์—์„œ ๋ฌธ์ œ๋ฅผ ์ผ์œผ์ผฐ๋˜ ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜์ •ํ•ด๋ณด์ž

 

@PostMapping("/addForm")
public String addCafe(@ModelAttribute Cafe cafe){
    cafeRepository.save(cafe);
    return "redirect:/basicCafe/cafes"+ cafe.getId();
}

 ์ด๋ ‡๊ฒŒ ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ฅผ ์ ์šฉ์‹œํ‚ด์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ

"cafe.getId() ์ฒ˜๋Ÿผ URL์— ๋ณ€์ˆ˜๋ฅผ ๋”ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ URL ์ธ์ฝ”๋”ฉ์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„ํ—˜ํ•˜๋‹ค."

์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด ๋ณ€์ˆ˜์— ๋„์›Œ์“ฐ๊ธฐ๋‚˜ ํ•œ๊ธ€์ด ํฌํ•จ๋  ๊ฒฝ์šฐ ์„œ๋ฒ„๊ฐ€ ์ธ์‹ํ•  ์ˆ˜ ์žˆ๊ฒŒ url์ธ์ฝ”๋”ฉ์„ ํ•ด์ฃผ์–ด์•ผํ•˜๋Š”๋ฐ 

์ด๊ฒƒ์ด ์•ˆ๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

 

๊ฒฐ๊ตญ ๋˜ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์ƒ๊ฒผ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ด๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋‘๊ฐ€์ง€ ํ•ด๊ฒฐ์ฑ…์ด ์ œ์‹œ๋˜์—ˆ๋‹ค.

 

์ฒซ๋ฒˆ์งธ, @PathVariable ์‚ฌ์šฉ

 

@PostMapping("/edit/{cafeId}")
public String edit(@PathVariable Long cafeId,@ModelAttribute Cafe cafe){
    cafeRepository.update(cafeId,cafe);
    return "redirect:/basic/items/{cafeId}";
}

 

@PathVariable ์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด ์ปจํŠธ๋กค๋Ÿฌ์— ๋งคํ•‘๋œ ๊ฐ’๋„ Redirect ๊ฐ’์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๋‘๋ฒˆ์งธ, RedirectAttributes ์‚ฌ์šฉ

 

@PostMapping("/addForm")
public String addCafe(@ModelAttribute Cafe cafe, RedirectAttributes redirectAttributes){
    Cafe savedCafe = cafeRepository.save(cafe);
    redirectAttributes.addAttribute("cafeId",savedCafe.getId());
    redirectAttributes.addAttribute("status",true);
    return "redirect:/basicCafe/items/{cafeId}";
}

 

RedirectAttributes ๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด, URL ์ธ์ฝ”๋”ฉ๋„ ํ•ด์ฃผ๊ณ , pathVarible , ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ๊นŒ์ง€ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

์ด ๋ฉ”์„œ๋“œ๋Š” 

localhost:8080/basic/cafes/3?status=true

๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.

 

๊ตณ์ด PathVariable์„ ์“ธ ํ•„์š”๋„ ์—†๊ณ  url ์ธ์ฝ”๋”ฉ ๊ฑฑ์ •๋„ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. 

๊ฒŒ๋‹ค๊ฐ€ status ํŒŒ๋ผ๋ฏธํ„ฐ๊นŒ์ง€ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค !! (๋ฐ”์ธ๋”ฉ ๋˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ)

ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ํƒ€์ž„๋ฆฌํ”„ ๋ฌธ๋ฒ•์—์„œ ${param.status}๋กœ ์กฐํšŒ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค