SpringMVC

    PRG Post/Redirect/Get

    PRG Post/Redirect/Get

    ๋„์ž… ๋ฌผ๊ฑด์„ ์ฃผ๋ฌธํ•˜๋Š” ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•˜๊ฑฐ๋‚˜ ํšŒ์›์„ ๋“ฑ๋กํ•˜๋Š” ์„œ๋น„์Šค๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ HTTP์˜ Post ๋ฉ”์†Œ๋“œ๋ฅผ ์ฃผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ๋•Œ, Post๋กœ ์ƒํ’ˆ์„ ์ฃผ๋ฌธํ•˜๋„๋ก ํ•˜๊ณ , ์›นํŽ˜์ด์ง€๋ฅผ ๋ฐ”๋กœ ๋žœ๋”๋งํ–ˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋งค์šฐ ๋”์ฐํ•œ ์ผ์ด ๋ฒŒ์–ด์ง„๋‹ค. ์™œ๋ƒ๋ฉด ์ตœ์ข…์ ์ธ HTTP๊ฐ€ Post์ด๊ธฐ ๋•Œ๋ฌธ์— ๋งŒ์•ฝ ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒฐ์ œ ํ›„ ๋žœ๋”๋ง๋œ ํŽ˜์ด์ง€์—์„œ ์ƒˆ๋กœ๊ณ ์นจ์„ ๋ˆ„๋ฅด๋ฉด ์ค‘๋ณตํ•ด์„œ ๊ฒฐ์ œ๊ฐ€ ์ง„ํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. Post - Redirect - Get PRG๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ์ด์œ ๋Š” ๋„์ž…๋ถ€๋ถ„์—์„œ ๋งค์šฐ ์ž์„ธํ•˜๊ฒŒ ์„ค๋ช…์ด ๋˜์–ด์žˆ๋‹ค. ํด๋ผ์ด์–ธํŠธ๊ฐ€ form tag์— ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ฃผ๋ฌธ์„ ํ•˜๋ฉด Post๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋„˜์–ด์™€ ์„œ๋ฒ„์—์„œ ํ•ด๋‹น ๋ถ€๋ถ„์„ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค. ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌ๊ฐ€ ๋œ ๊ฒฝ์šฐ ์ตœ์ข…์ ์ธ HTTP์ž…๋ ฅ์ด Post์ด๋ฏ€๋กœ ์ƒˆ๋กœ๊ณ ์นจ์‹œ ๊ณ„์†ํ•ด์„œ Post๊ฐ€ ์‹คํ–‰๋˜๋Š”..

    SSR Response, Response Body

    SSR Response, Response Body

    ๋„์ž… ์„œ๋ฒ„์˜ ๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ํŽ˜์ด์ง€๋ฅผ ๊ตฌ์ƒํ•˜์—ฌ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์„ SSR(Server Side Rendering)์ด๋ผ๊ณ  ํ•œ๋‹ค. ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ข…๋ฅ˜๊ฐ€ ์žˆ์ง€๋งŒ ๋‚˜๋Š” ์ฃผ๋กœ Thymeleaf๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์ด๋ฒˆ์— ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์€ ๋™์ ์ธ ์›นํŽ˜์ด์ง€๋ฅผ ์œ„ํ•ด SpringMVC์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ๋ฒ•๊ณผ Response Body๋ฅผ ์•Œ์•„๋ณด๊ฒ ๋‹ค. View๋กœ ๋ฐ์ดํ„ฐ ๋„˜๊ฒจ์ฃผ๋Š” ๋ฐฉ๋ฒ• Thymeleaf๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๋Š” ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ณ ์ž ํ•œ๋‹ค. @RequestMapping("/response-view-v1") public ModelAndView responseViewV1(){ ModelAndView mav = new ModelAndView("response/hello") .addObject("data","hello!");..

    Request Body ๋ฐ›์•„์˜ค๊ธฐ

    Request Body ๋ฐ›์•„์˜ค๊ธฐ

    Client๊ฐ€ Server๋กœ ํ˜น์€ Server๊ฐ€ Server๋กœ Request๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Request๊ฐ€ ์™”์„ ๋•Œ Body๋ฅผ String๊ณผ Json๋ฐฉ์‹์œผ๋กœ ๋ฐ›์•„์˜ค๋Š” ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ํ™•์ธํ•˜๊ฒ ๋‹ค. ๊น€์˜ํ•œ๋‹˜์˜ MVC ๊ฐ•์˜๋ฅผ ์ˆ˜๊ฐ• ํ›„ ์ž‘์„ฑ๋œ ํฌ์ŠคํŒ…์ด๋‹ค. 1. Body๊ฐ€ String์ธ ๊ฒฝ์šฐ Servlet์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Servlet์ฒ˜๋Ÿผ ๋ฐ›๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ด ๊ฒฝ์šฐ ๋ถˆํ•„์š”ํ•˜๊ฒŒ HttpServletRequest๋ฅผ ๋ชจ๋‘ ๋ฐ›์•„์•ผํ•˜๋ฏ€๋กœ ๋น„ํšจ์œจ์ ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์›ํ•˜๋Š” ๋ถ€๋ถ„๋งŒ ๋ฐ›์•„์„œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. @PostMapping("/request-body-string") public void requestBodyString(InputStream inputStream, Writer resp..

    Request Parameter ๊ฐ€์ ธ์˜ค๊ธฐ

    Request Parameter ๊ฐ€์ ธ์˜ค๊ธฐ

    ๋„์ž… Spring MVC๋Š” ๊ต‰์žฅํžˆ ํŽธ๋ฆฌํ•˜๊ฒŒ Request Parameter๋ฅผ ๋ฐ›์„์ˆ˜ ์žˆ๋‹ค. ๊ณผ๊ฑฐ Servlet์—์„œ๋Š” HttpRequest ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ getParameter๋ฅผ ๋ฐ›์•˜๋˜ ๊ฒƒ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๊ณ , ๋”์šฑ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ฐ›์„ ์ˆ˜ ๋„ ์žˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Servlet์—์„œ Parameter๋ฅผ ๋ฐ›๋Š” ๊ฒƒ์„ ์‹œ์ž‘์œผ๋กœ Spring MVC๊ฐ€ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•๊นŒ์ง€ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค. Request Parameter ๋ฐ›๋Š” ๋ฐฉ๋ฒ• 1. Servlet์ฒ˜๋Ÿผ ๋ฐ›๊ธฐ @RequestMapping("/request-param") public void requestParam(HttpServletRequest request, HttpServletResponse response) throws IOException { Stri..