Spring MVC

MVC ํŒจํ„ด Model2(Web MVC) ์š”์ฒญ ํ๋ฆ„

Web MVC Model2

์Šคํ”„๋ง MVC๋Š” ์œ„์™€ ๊ฐ™์€ Model2 ๊ตฌ์กฐ์˜ MVC Architecture์™€ Front Controller Pattern์„ ์ง€์›ํ•œ๋‹ค.

Spring MVC ๊ตฌ์„ฑ ์š”์†Œ ๋ฐ ์‹คํ–‰ ์ˆœ์„œ

Spring MVC

  1. DispatcherServlet(Front Controller)

    • ๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ตœ์ดˆ๋กœ ์ „๋‹ฌ๋ฐ›๋Š”๋‹ค.
    • ์š”์ฒญ์„ ์ˆ˜์‹ ํ•˜๊ณ  ์ฒ˜๋ฆฌ๋Š” ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์œ„์ž„ํ•œ๋‹ค. ๋˜ํ•œ ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์‘๋‹ต์œผ๋กœ ๋ฐœ์‹ ํ•œ๋‹ค.
    • Controller์—๊ฒŒ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ์ „๋‹ฌํ•˜๊ณ , Controller๊ฐ€ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์„ View์—๊ฒŒ ์ „๋‹ฌํ•˜์—ฌ ์š”์ฒญ์— ๋”ฐ๋ฅธ ์‘๋‹ต์„ ์ƒ์„ฑํ•œ๋‹ค.
  2. HandlerMapping

    • DispatcherServlet์ด ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์š”์ฒญ์„ ์–ด๋–ค Controller์—๊ฒŒ ์ „๋‹ฌํ•ด์•ผํ•  ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.
    • URL, ์š”์ฒญ ์ •๋ณด ๋“ฑ์„ ๊ธฐ์ค€์œผ๋กœ ์–ด๋–ค ํ•ธ๋“ค๋Ÿฌ ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•  ์ง€ ๊ฒฐ์ •ํ•œ๋‹ค.
  3. Controller

    • ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•œ ๋’ค, ๋น„์ฆˆ๋‹ˆ์Šค, ๋ฐ์ดํ„ฐ ๊ด€๋ จ ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด Model์„ ํ˜ธ์ถœํ•œ๋‹ค. ๋‹ค์‹œ Model๋กœ๋ถ€ํ„ฐ ๋ฐ˜ํ™˜๋ฐ›์€ ๊ฒฐ๊ณผ๋ฅผ DispatcherServlet์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.
  4. ModelAndView

    • Controller๊ฐ€ ์ฒ˜๋ฆฌํ•œ ๋ฐ์ดํ„ฐ ๋ฐ ํ™”๋ฉด์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ณด์œ ํ•œ ๊ฐ์ฒด
  5. ViewResolver

    • Controller์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค„ View๋ฅผ ๊ฒฐ์ •ํ•œ๋‹ค.
  6. View

    • Controller์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ค„ ์‘๋‹ต ํ™”๋ฉด์„ ์ƒ์„ฑํ•œ๋‹ค.

Controller

Client์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์—ญํ• 

ํด๋ž˜์Šค ์ƒ๋‹จ์— @Controller ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜์—ฌ Controller์ž„์„ ๋ช…์‹œํ•œ๋‹ค.

Controller ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” servlet-context.xml ํŒŒ์ผ ๋‚ด์— <bean> ํƒœ๊ทธ๋กœ ๊ฐ์ฒด๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ, ์ž๋™์œผ๋กœ ์Šค์บ”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

์ž๋™์œผ๋กœ ์Šค์บ”ํ•˜๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” servlet-context.xml ํŒŒ์ผ ๋‚ด์— <context:component-scan> ํƒœ๊ทธ๋ฅผ ์„ ์–ธํ•œ๋‹ค. ์†์„ฑ์œผ๋กœ base-package๋ฅผ ์„ค์ •ํ•˜์—ฌ ํ•ด๋‹น ํŒจํ‚ค์ง€ ๋‚ด์— @Controller ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋ฅผ ์ž๋™์œผ๋กœ ์ฐพ๋„๋ก ํ•œ๋‹ค.

Model

Business์™€ DataBase ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์—ญํ• 

์ฃผ๋กœ DAO, DTO, Service ๋“ฑ์ด ํ•ด๋‹น๋œ๋‹ค.

DAO ํด๋ž˜์Šค ์ƒ๋‹จ์—๋Š” @Repository, Service ํด๋ž˜์Šค ์ƒ๋‹จ์—๋Š” @Service ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•œ๋‹ค.