프로젝트/Spring

[Spring 스프링] Spring MVC(Model, View, Controller)

yepppi 2023. 1. 9. 13:21
반응형
SMALL

Spring MVC란?

Spring에서 제공하는 웹 모듈로, MVC란 각각이 Model, View, Controller인 것이다.

 

Model: View에 출력할 데이터를 담아두는 구성요소

View: 말 그대로 화면에 보여지는 것에 집중한 구성요소

Controller: 받은 요청에 맞게 비즈니스 로직을 실행하여 반환해주는 구성요소

 

Model과 View는 서로의 존재를 모른다. Controller가 그 사이에서 변경을 감지하고 해결해주는 역할.

 

 

Spring MVC 구조는 크게 DispatcherServlet(Front Controller), Handler(Controller), ModelAndView, ViewResolver 4가지로 나뉜다.

 

DispatcherServlet(Front Controller): 가장 앞단에서 HTTP 요청 처리. DispatcherServlet이 해당 요청을 처리할 Controller를 지정해주는 역할.

Handler(Controller): HTTP 요청을 처리하는 역할. 필요한 데이터를 Model에 저장하고 View를 반환.

ModelAndView: Controller에 의해 반환된 Model과 View가 함께 들어있는 객체

ViewResolver: ModelAndView를 처리하며 Model에 저장되어 있는 데이터를 바탕으로 View를 반환.

 

간단하게 원리를 보자면, 웹에서 요청받은 URL을 톰캣이 스프링 컨테이너에 건네주면, 컨테이너는 해당 URL과 mapping 되어 있는 controller를 호출 후 처리하여 html 파일 이름을 반환한다. controller에서 model에 데이터를 넣어주고 viewResolver는 resources 패키지에서 templates 안의 해당 이름의 파일을 찾고 thymeleaf 템플릿 엔진이 html 파일로 변환 후 웹에 넘겨준다.

 

thymeleaf 개념에 관해서는 아래 글에서 확인할 수 있다.

2023.01.03 - [공부 기록장/Spring Boot] - [Spring 스프링] Thymeleaf(타임리프)란?

 

 

Spring MVC 공식 문서

공식 문서에 자세하게 나와 있다.

Spring Boot Features

 

Spring Boot Features

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

 

반응형
LIST