Spring/Spring MVC

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 18. Redirect / Post -> Redirect -> Get

๋ฆฌ๋‹ค์ด๋ ‰ํŠธ๋ž€ ๋ง ๊ทธ๋Œ€๋กœ re(๋‹ค์‹œ) + ์ง€์‹œํ•˜๋‹ค(direct) ๋‹ค์‹œ ์ง€์‹œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ www.test.com/page1 URL์„ ์›น ์„œ๋ฒ„์— ์š”์ฒญํ–ˆ์Šต๋‹ˆ๋‹ค. ์„œ๋ฒ„๋Š” HTTP ์‘๋‹ต ๋ฉ”์‹œ์ง€๋ฅผ ํ†ตํ•ด "www.test.com/page2 ๋กœ ๋‹ค์‹œ ์š”์ฒญํ•˜์„ธ์š”~" ๋ผ๊ณ  ๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ๋‹ค๋ฅธ URL(๊ธธ, ๋ฐฉํ–ฅ) ์„ ์ง€์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. POST ๋“ฑ๋ก ํ›„ ์ƒˆ๋กœ ๊ณ ์นจ ์›น ๋ธŒ๋ผ์šฐ์ €์˜ ์ƒˆ๋กœ ๊ณ ์นจ์€ ๋งˆ์ง€๋ง‰์— ์„œ๋ฒ„์— ์ „์†กํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ์ „์†กํ•œ๋‹ค. ์ƒํ’ˆ ๋“ฑ๋ก ํผ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์ €์žฅ์„ ์„ ํƒํ•˜๋ฉด POST /add + ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ์ „์†กํ•œ๋‹ค. ์ด ์ƒํƒœ์—์„œ ์ƒˆ๋กœ ๊ณ ์นจ์„ ๋˜ ์„ ํƒํ•˜๋ฉด ๋งˆ์ง€๋ง‰์— ์ „์†กํ•œ POST /add + ์ƒํ’ˆ ๋ฐ์ดํ„ฐ๋ฅผ ์„œ๋ฒ„๋กœ ๋‹ค์‹œ ์ „์†กํ•˜๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ๋‚ด์šฉ์€ ๊ฐ™๊ณ , ID๋งŒ ๋‹ค๋ฅธ ์ƒํ’ˆ ๋ฐ์ดํ„ฐ..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 17. @ModelAttribute / redirect

์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌ @ModelAttribue Item item @PostMapping("/add") public String addItem(@ModelAttribute Item item,Model model) { itemRepository.save(item); model.addAttribute("item",item); return "basic/item"; } ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฐ›์•„์„œ ํ•„์š”ํ•œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ๊ฐ์ฒด์— ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ธฐ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ ํ”„๋กœํผํ‹ฐ ์ ‘๊ทผ๋ฒ•(setXxx)์œผ๋กœ ์ž…๋ ฅํ•ด์ค€๋‹ค. public String addItemV1(@RequestParam String itemName, @RequestParam int price, @RequestParam Integer quantity, Model ..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 16. Thymeleaf ํƒ€์ž„๋ฆฌํ”„ ์‚ฌ์šฉ๋ฒ• ๋ฐ ๋ฌธ๋ฒ•

์ •์ ์ธ html ์„ ๋™์  html ๋กœ ๋ฐ”๊ฟ”๋ณด์ž ํƒ€์ž„๋ฆฌํ”„๋Š” ์ˆœ์ˆ˜ HTML ํŒŒ์ผ์„ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์—ด์–ด๋„ ๋‚ด์šฉ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ , ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ๋ทฐ ํ…œํ”Œ๋ฆฟ์„ ๊ฑฐ์น˜๋ฉด ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝ๋œ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ์ˆœ์ˆ˜ HTML์„ ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ทฐ ํ…œํ”Œ๋ฆฟ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํƒ€์ž„๋ฆฌํ”„์˜ ํŠน์ง•์„ ๋„ค์ธ„๋Ÿด ํ…œํ”Œ๋ฆฟ ํƒ€์ž„๋ฆฌํ”„ ๊ฐ„๋‹จํžˆ ์•Œ์•„๋ณด๊ธฐ ํƒ€์ž„๋ฆฌํ”„ ์‚ฌ์šฉ ์„ ์–ธ ์†์„ฑ ๋ณ€๊ฒฝ - th:href ๋ทฐ ํ…œํ”Œ๋ฆฟ์„ ๊ฑฐ์น˜๋ฉด th:href ์˜ ๊ฐ’์ด href ๋กœ ๋Œ€์ฒด๋˜๋ฉด์„œ ๋™์ ์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ HTML ์†์„ฑ์„ th:xxx ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค. ์ƒํ’ˆ ๋ชฉ๋ก ํŽ˜์ด์ง€ ์ƒํ’ˆ ๋“ฑ๋ก ๋ฒ„ํŠผ html ์ƒํ’ˆ ๋“ฑ๋ก ์†์„ฑ ๋ณ€๊ฒฝ - th:onclick onclick="location.href='addForm.html'" th:onclick="|locati..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 15. HTTP ์‘๋‹ต / HttpMessageConverter / ArgumentResolver

์Šคํ”„๋ง(์„œ๋ฒ„)์—์„œ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• ์ •์  ๋ฆฌ์†Œ์Šค ์˜ˆ) ์›น ๋ธŒ๋ผ์šฐ์ €์— ์ •์ ์ธ HTML, css, js๋ฅผ ์ œ๊ณตํ•  ๋•Œ๋Š”, ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ทฐ ํ…œํ”Œ๋ฆฟ ์‚ฌ์šฉ ์˜ˆ) ์›น ๋ธŒ๋ผ์šฐ์ €์— ๋™์ ์ธ HTML์„ ์ œ๊ณตํ•  ๋•Œ๋Š” ๋ทฐ ํ…œํ”Œ๋ฆฟ์„ ์‚ฌ์šฉํ•œ๋‹ค. HTTP ๋ฉ”์‹œ์ง€ ์‚ฌ์šฉ HTTP API๋ฅผ ์ œ๊ณตํ•˜๋Š” ๊ฒฝ์šฐ์—๋Š” HTML์ด ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•ด์•ผ ํ•˜๋ฏ€๋กœ, HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— JSON ๊ฐ™์€ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์‹ค์–ด ๋ณด๋‚ธ๋‹ค. HTTP ์‘๋‹ต - ์ •์  ๋ฆฌ์†Œ์Šค, ๋ทฐ ํ…œํ”Œ๋ฆฟ ์ •์  ๋ฆฌ์†Œ์Šค ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ํด๋ž˜์ŠคํŒจ์Šค์˜ ๋‹ค์Œ ๋””๋ ‰ํ† ๋ฆฌ์— ์žˆ๋Š” ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ ์ œ๊ณตํ•œ๋‹ค. /static , /public , /resources , /META-INF/resources src/main/resources ๋ทฐ ํ…œํ”Œ๋ฆฟ ๋ทฐ ํ…œํ”Œ๋ฆฟ์„ ๊ฑฐ์ณ์„œ HTML์ด ์ƒ..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 14. HTTP message body ๋ฅผ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ์š”์ฒญ

HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€ - ๋‹จ์ˆœ ํ…์ŠคํŠธ ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ์™€ ๋‹ค๋ฅด๊ฒŒ, HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๊ฐ€ ์ง์ ‘ ๋„˜์–ด์˜ค๋Š” ๊ฒฝ์šฐ๋Š” @RequestParam , @ModelAttribute ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. InputStream ServletInputStream inputStream = request.getInputStream(); String messageBody = StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8); log.info("messageBody={}",messageBody); HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์˜ ๋ฐ์ดํ„ฐ๋ฅผ InputStream ์„ ์‚ฌ์šฉํ•ด์„œ ์ง์ ‘ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. @PostMapping("/request-body-string-v2") pu..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 13. ์š”์ฒญ ๋งคํ•‘ / ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ

์š”์ฒญ ๋งคํ•‘ @RestController public class MappingController { @RequestMapping("/hello-basic") public String helloBasic() { return "OK"; } @RestController @Controller ๋Š” ๋ฐ˜ํ™˜ ๊ฐ’์ด String ์ด๋ฉด ๋ทฐ ์ด๋ฆ„์œผ๋กœ ์ธ์‹๋œ๋‹ค. ๊ทธ๋ž˜์„œ ๋ทฐ๋ฅผ ์ฐพ๊ณ  ๋ทฐ๊ฐ€ ๋žœ๋”๋ง ๋œ๋‹ค. @RestController ๋Š” ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ ๋ทฐ๋ฅผ ์ฐพ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, HTTP ๋ฉ”์‹œ์ง€ ๋ฐ”๋””์— ๋ฐ”๋กœ ์ž…๋ ฅํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์‹คํ–‰ ๊ฒฐ๊ณผ๋กœ ok ๋ฉ”์„ธ์ง€๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. @RequestMapping("/hello-basic") /hello-basic URL ํ˜ธ์ถœ์ด ์˜ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋˜๋„๋ก ๋งคํ•‘ํ•œ๋‹ค. @RequestMapping ์— metho..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 12. ๋กœ๊ทธ / SLF4J / Logback

Welcome ํŽ˜์ด์ง€ ๋งŒ๋“ค๊ธฐ ์Šคํ”„๋ง ๋ถ€ํŠธ์— Jar ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด /resources/static/ ์œ„์น˜์— index.html ํŒŒ์ผ์„ ๋‘๋ฉด Welcome ํŽ˜์ด์ง€๋กœ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค. ๋กœ๊น… ๊ฐ„๋‹จํžˆ ์•Œ์•„๋ณด๊ธฐ ๋ณ„๋„์˜ ๋กœ๊น… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋กœ๊ทธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์Šคํ”„๋ง ๋ถ€ํŠธ ๋กœ๊น… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ( spring-boot-starter-logging ) ๋กœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” Logback, Log4J, Log4J2 ๋“ฑ๋“ฑ ์ˆ˜ ๋งŽ์€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ๋Š”๋ฐ, SLF4J๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๊ณ , ๊ทธ ๊ตฌํ˜„์ฒด๋กœ Logback ๊ฐ™์€ ๋กœ๊ทธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์„ ํƒํ•˜๋ฉด ๋œ๋‹ค. ๋กœ๊ทธ ์„ ์–ธ private final Logger log = LoggerFactory.getLogger(getClass()); @Slf4j public class LogTestController ..

Spring/Spring MVC

[์Šคํ”„๋ง MVC] - 11. @Controller / @RequestMapping

์Šคํ”„๋ง MVC ๊ตฌ์กฐ @RequestMapping ์• ๋…ธํ…Œ์ด์…˜์„ ํ™œ์šฉํ•œ ๋งค์šฐ ์œ ์—ฐํ•˜๊ณ , ์‹ค์šฉ์ ์ธ ์ปจํŠธ๋กค๋Ÿฌ @RequestMapping ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์ด๋‹ค. ๊ฐ€์žฅ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ @RequestMapping RequestMappingHandlerMapping RequestMappingHandlerAdapter @Controller public class SpringMemberFormControllerV1 { @RequestMapping("/springmvc/v1/members/new-form") public ModelAndView process() { return new ModelAndView("new-form"); } } @Controller ์Šคํ”„๋ง์ด ์ž๋™์œผ๋กœ ์Šคํ”„..

hello_u
'Spring/Spring MVC' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก