๐Ÿ’ป ๊ณต๋ถ€ ๊ธฐ๋ก/๐Ÿƒ Spring

Spring | RedirectAttributes

  • -
  • RedirectAttributes ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ „๋‹ฌ
  • RedirectAttributes ํด๋ž˜์Šค๋Š” Spring 3.1 ๋ฒ„์ „์— ์ถ”๊ฐ€๋จ
  • RedirectAttributes ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด String ํ˜•ํƒœ๋ฅผ ํฌํ•จํ•œ map, list, vo ๋“ฑ์˜ Object ํ˜•ํƒœ๋กœ ์ „๋‹ฌ ๊ฐ€๋Šฅ

 

@Controller public class TestAttributes { @RequestMapping(value="/aaa") public String insertA(ModelMap model, RedirectAttributes rttr) throws Exception{ String message ="๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค."; rttr.addAttribute("message", message); return "redirect:/abc"; } }

 

message ์— ๋‹ด์€ ๊ฐ์ฒด๋ฅผ redirect ๋ฅผ ํ†ตํ•ด์„œ ๋ณด๋‚ด๊ณ ์ž ํ•˜๋Š” ๊ฒฝ๋กœ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋ƒˆ๋‹ค.

/abc?message=๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

์‹œํ–‰์ฐฉ์˜ค | method = RequestMethod.POST
redirect ์‹œ  RequestMethod.POST ๋กœ ๋ณด๋‚ด๊ฒŒ ๋  ๊ฒฝ์šฐ (postman ์œผ๋กœ๋Š” ํ™•์ธ ๊ฐ€๋Šฅ)
// WARN : org.springframework.web.servlet.PageNotFound - Request method 'GET' not supported
RequestMethod.GET ์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

๊ฒŒ์‹œ๊ธ€์˜ ๋“ฑ๋ก/์ˆ˜์ •/์‚ญ์ œ ๋“ฑ ๋ณด์•ˆ์— ๋ฏผ๊ฐํ•œ ๊ธฐ๋Šฅ์— ๋งŽ์ด ์‚ฌ์šฉ๋จ
ํœ˜๋ฐœ์„ฑ ๋ฐ์ดํ„ฐ(์ƒˆ๋กœ๊ณ ์นจ ์‹œ ๋ฐ์ดํ„ฐ ์‚ฌ๋ผ์ง)
@Controller public class TestAttributes { @RequestMapping(value="/aaa") public String insertA(ModelMap model, RedirectAttributes rttr) throws Exception{ String message ="๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค."; rttr.addFlashAttribute("message", message); return "redirect:/bbb"; } }

 

addAttribute ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ GET ์œผ๋กœ ๋ฐ›์€ ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋Š” URL ์— ๊ณ ์Šค๋ž€ํžˆ ๋‚จ์•„์žˆ๋˜ ๋ฐ˜๋ฉด์—

addFlashAttribute ๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ POST ๋กœ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜์—ฌ URL ์—๋Š” ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.

post url ํ˜•ํƒœ

 

๊ทธ๋ ‡๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ๋‹ด์€ ๋ฐ์ดํ„ฐ๋Š” ์–ด๋–ป๊ฒŒ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š”๊ฐ€?

 

" @ModelAttribute("message") String text "
addAttribute/addFlashAttribute ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ๋ณด๋‚ด๊ณ , ํŒŒ๋ผ๋ฏธํ„ฐ Key ๋ฅผ ๋ฐ›์•„ ํƒ€์ž…์— ๋งž๊ฒŒ ๊ฐ์ฒด์— ๋‹ด์•„์„œ ์‚ฌ์šฉํ•œ๋‹ค.
@Controller public class TestAttributes { @RequestMapping(value="/bbb") protected void insertB(@ModelAttribute("message") String text, ModelMap model, RedirectAttributes rttr, HttpServletRequest request, HttpServletResponse response) throws Exception{ // ํ•œ๊ธ€ ์ธ์ฝ”๋”ฉ response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); // ํ™”๋ฉด ์ถœ๋ ฅ ์ฝ”๋“œ PrintWriter out = response.getWriter(); String textResult = text; // rttr ๊ฐ์ฒด ๋ฐ์ดํ„ฐ out.println("<html><head><title>์•ˆ๋…•ํ•˜์„ธ์š”</title></head><body>"); out.println("<h1>Hello</h1>"); out.println("<h1>"+ textResult + "</h1>"); out.println("</body><html>"); } }

 

RequestContextUtils.getInputFlashMap(request)
์ถœ๋ ฅ(print)
// FlashMap [attributes={message=๋“ฑ๋ก๋˜์—ˆ์Šต๋‹ˆ๋‹ค.}, targetRequestPath=/ch2/bbb, targetRequestParams={}]
Map<String, ?> flashMap = RequestContextUtils.getInputFlashMap(request);

 

 ${ key }
๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ ์ž ํ•˜๋Š” ' .jsp ' ์— ${ key } ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, addAttribute/addFlashAttribute ์œผ๋กœ ๋ณด๋‚ธ ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
${ message }

 

 

 

์ถœ์ฒ˜ | https://web-obj.tistory.com/455

์ถœ์ฒ˜ | https://galid1.tistory.com/563

์ถœ์ฒ˜ | https://m.blog.naver.com/allkanet72/220964699929

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.