2 ๋ถ„ ์†Œ์š”

Spring MVC๋ž€?

Spring MVC๋Š” MVCํŒจํ„ด์„ ์‚ฌ์šฉํ•ด ์›น์„ ๊ตฌ์„ฑ(์‚ฌ์šฉ์ž ์š”์ฒญ์„ ์ฒ˜๋ฆฌ)ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” Spring์ด ์ง€์›ํ•˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค.

Spring MVC์˜ ๊ตฌ์กฐ

Spring MVC์˜ ์ฃผ์š” ๊ตฌ์„ฑ์š”์†Œ๋Š” Model, View Controller์ด๋‹ค.
ํ•˜์ง€๋งŒ, ์ด๋“ค์ด ์œ ๊ธฐ์ ์œผ๋กœ ๋™์ž‘ํ•˜๋ ค๋ฉด ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

DispatcherServlet(Front Controller)

DispatcherServlet์ด๋ž€, ๊ฐ€์žฅ ์•ž๋‹จ์—์„œ HTTP Request๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” Controller์ด๋‹ค.
๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•˜๋ฉด ์‚ฌ์šฉ์ž ์š”์ฒญ์„ ๊ฐ€์žฅ ๋จผ์ € ๋ฐ›์•„, ํ•ด๋‹น ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  Controller๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ์—ญํ• ์ด๋‹ค.

Handler(Controller)

Handler๋Š” DistpatcherServlet์— ์˜ํ•ด ๋ฐฐ์ •๋œ Controller์ด๋‹ค.
Controller๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ Model์„ ํ˜ธ์ถœํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ์—ฐ๊ด€๋œ ์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋„๋ก ํ•˜๊ฑฐ๋‚˜, View๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

ModelAndView

ModelAndView๋Š” Handler ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ํ›„ ์‘๋‹ตํ•œ View์™€ View์— ์ „๋‹ฌํ•  ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค.
๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒ์„ฑ์ž๋“ค์ด ํ•„์š”ํ•˜๋‹ค.
ModelAndView(String viewName) : ์‘๋‹ตํ•  view ์„ค์ •
ModelAndView(String viewName, Map values) : ์‘๋‹ตํ•  view์™€ view๋กœ ์ „๋‹ฌํ•  ๊ฐ’๋“ค์„ ์ €์žฅํ•œ Map ๊ฐ์ฒด
ModelAndView(String viewName, String name, Object value) : ์‘๋‹ตํ•  view ์ด๋ฆ„, view๋กœ ๋„˜๊ธธ ๊ฐ์ฒด์˜ name-value

View Resolver

View Resolver๋ž€ ์•ž์„œ Handler์—์„œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ ํ›„, ModelAndView๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ์ด ํ›„์— ModelAndView๋ฅผ ์•Œ๋งž์€ View๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด DispatcherServlet์— ์˜ํ•ด View Resolver๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

View Resolver๋Š” ModelAndView ๊ฐ์ฒด๋ฅผ View ์˜์—ญ์œผ๋กœ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด ์•Œ๋งž์€ View ์ •๋ณด๋ฅผ ์„ค์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

๋™์ž‘ ์ˆœ์„œ

image

MVC

MVC๋ž€ Model View Controller์˜ ์•ฝ์ž๋กœ ๋””์ž์ธ ํŒจํ„ด(design pattern)์ค‘ ํ•˜๋‚˜์ด๋‹ค.
Model, View, Controller๋Š” ๊ฐ์ž ์ˆ˜ํ–‰ํ•˜๋Š” ์—ญํ• ์ด ๋‹ค๋ฅธ๋ฐ, ์ด ์ปจํ…Œ์ด๋„ˆ๋“ค์ด ์—ฐ๊ฒฐ๋˜์–ด ํ•˜๋‚˜์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค.

image

Model

๋ชจ๋ธ(Model) ์€ ์š”์ฒญ์— ๋”ฐ๋ฅธ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
ํ”ํžˆ DTO, DAO, VO์™€ ๊ฐ™์€ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

DAO(Data Access Object): ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๋Š” ๊ฐ์ฒด๋กœ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ ๋กœ์ง๊ณผ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๋ถ„๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
DTO(Data Transfer Object): ๊ณ„์ธต๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฐ์ฒด๋กœ, getter/setter ์™ธ์˜ ๋กœ์ง์€ ํ•„์š”ํ•˜์ง€ ์•Š๋‹ค.
VO(Value Object): ๊ฐ’ ๊ทธ ์ž์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋กœ, ๋กœ์ง์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๊ณ , ๋ถˆ๋ณ€์„ฑ ๋ณด์žฅ์„ ์œ„ํ•ด ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.

๋ชจ๋ธ์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์‚ฌ์šฉ์ž๊ฐ€ ํŽธ์ง‘ํ•˜๊ธธ ์›ํ•˜๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  2. View๋‚˜ Controller์— ๋Œ€ํ•ด ์–ด๋–ค ์ •๋ณด๋„ ์•Œ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
  3. ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚˜๋ฉด ๋ณ€๊ฒฝ ํ†ต์ง€์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฐฉ๋ฒ•์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

DTO (Data Transfer Object) : ๋ฐ์ดํ„ฐ ์ „๋‹ฌ
DAO (Data Access Object) : ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผ Service : DAO, DTO, ๊ฒ€์ฆ๋กœ์ง, ๊ธฐํƒ€๋กœ์ง์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•œ ๋ชจ๋ธ

View

๋ทฐ(View) ๋Š” ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋งํ•œ๋‹ค.
.html, .jsp, .mustache์™€ ๊ฐ™์€ ํŒŒ์ผ๋“ค๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ทฐ์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ๋ชจ๋ธ์ด ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ •๋ณด๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.
  2. ๋ชจ๋ธ์ด๋‚˜ ์ปจํŠธ๋กค๋Ÿฌ์™€ ๊ฐ™์ด ๋‹ค๋ฅธ ๊ตฌ์„ฑ์š”์†Œ๋“ค์„ ๋ชฐ๋ผ์•ผ ํ•œ๋‹ค.
  3. ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚˜๋ฉด ๋ณ€๊ฒฝ ํ†ต์ง€์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฐฉ๋ฒ•์„ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

Controller

์ปจํŠธ๋กค๋Ÿฌ(Controller) ๋Š” ์‚ฌ์šฉ์ž ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ผ์ข…์˜ ์ฐฝ๊ตฌ์˜ ์—ญํ• ์„ ํ•œ๋‹ค.

์ปจํŠธ๋กค๋Ÿฌ์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ๋ชจ๋ธ์ด๋‚˜ ๋ทฐ์— ๋Œ€ํ•ด ์•Œ๊ณ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  2. ๋ชจ๋ธ์ด๋‚˜ ๋ทฐ์˜ ๋ณ€๊ฒฝ์„ ๋ชจ๋‹ˆํ„ฐ๋ง ํ•ด์•ผ ํ•œ๋‹ค.

Spring MVC์™€ Thymeleaf ์—ฐ๋™ ์„ค์ •

โ€œhttps://mvnrepository.comโ€์—์„œ ์•„๋ž˜ ํ‚ค์›Œ๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์˜์กด์„ฑ ์ถ”๊ฐ€ ํ•„์š”

  • servlet-api
  • servlet-jsp.api
  • spring-webmvc
  • thymeleaf-spring5
  • thymeleaf-layout
  • thymeleaf-jsr310 (thymeleaf extras java8time)
  • sjf4j
  • Logback Classic Module
  • web.xml
    • servlet
    • filter

Thymeleaf(ํƒ€์ž„๋ฆฌํ”„)

ํƒ€์ž„๋ฆฌํ”„๋ž€, ์Šคํ”„๋ง๋ถ€ํŠธ์—์„œ ๊ณต์‹์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” view ํ…œํ”Œ๋ฆฟ์ด๋‹ค.
JSP์™€ ๋‹ฌ๋ฆฌ ํƒ€์ž„๋ฆฌํ”„ ๋ฌธ์„œ๋Š” html ํ™•์žฅ์ž๋ฅผ ๊ฐ–๊ณ ์žˆ์–ด ์„œ๋ฒ„ ์—…์‹ฑ๋„ ๋™์ž‘ ๊ฐ€๋Šฅํ•จ

์ฐธ๊ณ ์ž๋ฃŒ

Spring MVC Framework๋ž€ ๋ฌด์—‡์ธ๊ฐ€?
[JAVA] DTO์™€ VO์˜ ์ฐจ์ด
[Spring] ModelAndView
[Spring MVC] ์Šคํ”„๋ง MVC ๋ทฐ ๋ฆฌ์กธ๋ฒ„(View Resolver)
DAO, DTO, VO ๋ž€? ๊ฐ„๋‹จํ•œ ๊ฐœ๋… ์ •๋ฆฌ

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ:

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ