Spring MVC ๊ตฌ์กฐ
by rlaehddnd0422Spring MVC ์ ์ฒด ๊ตฌ์กฐ
Spring MVC ๋ ์ด ์ ์ ์ง์ ๋ง๋ค์๋ MVC ๊ตฌ์กฐ์ ๋งค์ฐ ํก์ฌํฉ๋๋ค.
์ด๋ฆ๋ง ์กฐ๊ธ ๋ค๋ฅผ ๋ฟ ๊ตฌ์กฐ์ ๋ก์ง์ ๋์ผํฉ๋๋ค.
์ด๋ฆ๋ง ๋ฐ๋์์ ๋ฟ ๊ฐ ํด๋์ค์ ์ญํ ์ ๋์ผํฉ๋๋ค
- Front-Controller โฃ DispatcherServlet
- handlerMappingMap โฃ HandlerMapping
- MyhandlerAdapter โฃ HandlerAdapter
- ModelView โฃ ModelAndView
- viewResolver โฃ ViewResolver
- MyView โฃ View
์ด ์ ํฌ์คํ ์์ ๋ง๋ค์๋ MVC ๊ตฌ์กฐ๋ฅผ ์ ๊ธฐ์ตํ๋ฉด์ ๊ฐ ํด๋์ค๋ค์ ์ญํ ์ ๊ธฐ์ตํ๋ฉด ๋ฉ๋๋ค.
Spring MVC ๋ํ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ ํจํด์ผ๋ก ๊ตฌํ๋์ด ์๋๋ฐ์.
Spring MVC์ ํ๋ก ํธ ์ปจํธ๋กค๋ฌ๊ฐ ๋ฐ๋ก DispatcherServlet์ ๋๋ค.
์ด ๋์คํจ์ณ ์๋ธ๋ฆฟ์ด ์คํ๋ง MVC์ ํต์ฌ์ ๋๋ค.
์ฐ์ DispatcherServlet์ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
DispatcherServlet ์๋ธ๋ฆฟ ๋ฑ๋ก
DistpatcherServlet์ ๋ถ๋ชจ ํด๋์ค์ธ HttpServlet์ ์์๋ฐ์ ์ฌ์ฉํ๊ณ , ์๋ธ๋ฆฟ์ผ๋ก ๋์ํฉ๋๋ค.
์๋ธ๋ฆฟ ์์๊ด๊ณ
- DispatcherServlet โฃ FrameworkServlet โฃ HttpServletBean โฃ HttpServlet
์คํ๋ง ๋ถํธ๋ DispatcherServlet์ ์๋ธ๋ฆฟ์ผ๋ก ์๋์ผ๋ก ๋ฑ๋กํ๊ณ ๋ชจ๋ ๊ฒฝ๋ก urlPatterns="/"์ ๋ํด ๋งคํํฉ๋๋ค.
์์ฒญ ํ๋ฆ
- ์๋ธ๋ฆฟ์ด ํธ์ถ๋๋ฉด HttpServlet์ด ์ ๊ณตํ๋ service()๊ฐ ํธ์ถ๋ฉ๋๋ค.
- ์คํ๋ง MVC๋ DispatcherServlet์ ๋ถ๋ชจ์ธ FrameworkServlet์์ service()๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํด๋์ต๋๋ค.
- ๋ฐ๋ผ์ FrameworkServlet.service()๋ฅผ ์์์ผ๋ก ์ฌ๋ฌ ๋ฉ์๋๊ฐ ํธ์ถ๋๋ฉด์ DispatcherServlet.doDispatch()๊ฐ ํธ์ถ๋ฉ๋๋ค.
์๋ธ๋ฆฟ ํธ์ถ โฃ HttpServlet.service() ํธ์ถ โฃ FrameworkServlet.service() ํธ์ถ โฃ DispatcherServlet.doDispatch()
DispatcherServlet.doDispatch() ์ฝ๋๋ฅผ ๋ถ์ํด๋ณด๊ฒ ์ต๋๋ค.
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
1. ํธ๋ค๋ฌ ์กฐํ
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
request์ ๋งคํ๋๋ ํธ๋ค๋ฌ(์ปจํธ๋กค๋ฌ)๊ฐ ์๋์ง ์กฐํํฉ๋๋ค.
2. ์ด๋ํฐ ์กฐํ
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
1์์ ์กฐํํ ํธ๋ค๋ฌ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋ ์ด๋ํฐ๊ฐ ์๋์ง ์กฐํํฉ๋๋ค.
3. ์ด๋ํฐ ์คํ โฃ ModelAndView ๋ฆฌํด
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
4. ๋ฆฌํด๋ ModelAndView๋ฅผ ์ด์ฉํด processDispatchResult ๋ฉ์๋ ์คํ
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
processDispatchResult ๋ฉ์๋ โฃ render ๋ฉ์๋ ํธ์ถ
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
@Nullable Exception exception) throws Exception {
render(mv, request, response);
}
render ๋ฉ์๋
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception
{
View view;
String viewName = mv.getViewName();
view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
view.render(mv.getModelInternal(), request, response);
}
5. ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ํตํด ๋ทฐ ์ฐพ๊ณ View ๋ฐํ
view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
6. View ๋ ๋๋ง
view.render(mv.getModelInternal(), request, response);
๋์ ์์
1. ํธ๋ค๋ฌ ์กฐํ : ํธ๋ค๋ฌ ๋งคํ์ ํตํด ์์ฒญ URL์ ๋ํด ๋งคํ๋ ์ปจํธ๋กค๋ฌ(ํธ๋ค๋ฌ) ์กฐํ
2. ํธ๋ค๋ฌ ์ด๋ํฐ ์กฐํ : ์กฐํํ ํธ๋ค๋ฌ๋ฅผ ์คํํ ์ ์๋ ์ด๋ํฐ ์กฐํ
3. ํธ๋ค๋ฌ ์ด๋ํฐ ์คํ : ์ด๋ํฐ ์คํ
4. ํธ๋ค๋ฌ ์คํ : ํธ๋ค๋ฌ ์ด๋ํฐ๊ฐ ํธ๋ค๋ฌ ์คํ
5. ModelAndView ๋ฐํ : ํธ๋ค๋ฌ๊ฐ ๋ฆฌํดํ๋ ์ ๋ณด๋ฅผ ModelAndView์ ๋ด์ ๋ฆฌํด
6. viewResolver ํธ์ถ : ๋ทฐ ๋ฆฌ์กธ๋ฒ๋ฅผ ์ฐพ๊ณ ์คํ
JSP์ ๊ฒฝ์ฐ InternalResourceViewResolver๊ฐ ์๋ ๋ฑ๋ก๋๊ณ ์ฌ์ฉ๋ฉ๋๋ค. ๋ค์์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค. ์ฐ์ ํจ์ค
7. View ๋ฐํ : ๋ทฐ ๋ฆฌ์กธ๋ฒ๊ฐ ๋ทฐ์ ๋ ผ๋ฆฌ ์ด๋ฆ์ ๋ฌผ๋ฆฌ ์ด๋ฆ์ผ๋ก ๋ฐ๊ฟ์ ๋ทฐ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํฉ๋๋ค.
8. ๋ทฐ ๋ ๋๋ง : ๋ทฐ๋ฅผ ํตํด์ ๋ทฐ๋ฅผ ๋ ๋๋ง ํฉ๋๋ค.
์ธํฐํ์ด์ค ์ดํด๋ณด๊ธฐ
Spring MVC์ ํฐ ๊ฐ์ : DispatcherServlet ์ฝ๋์ ๋ณ๊ฒฝ ์์ด ์ํ๋ ๊ธฐ๋ฅ์ ๋ณ๊ฒฝํ๊ฑฐ๋ ํ์ฅํ ์ ์์ต๋๋ค.
์ด ์ธํฐํ์ด์ค๋ค์ ๊ตฌํํด์ DispatcherServlet์ ๋ฑ๋ก๋งํ๋ฉด ์ปค์คํ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
์ฃผ์ ์ธํฐํ์ด์ค ๋ชฉ๋ก
- ํธ๋ค๋ฌ ๋งคํ: org.springframework.web.servlet.HandlerMapping
- ํธ๋ค๋ฌ ์ด๋ํฐ: org.springframework.web.servlet.HandlerAdapter
- ๋ทฐ ๋ฆฌ์กธ๋ฒ: org.springframework.web.servlet.ViewResolver
- ๋ทฐ: org.springframework.web.servlet.View
<์ ๋ฆฌ>
1. ์คํ๋ง MVC๋ ์ฝ๋ ๋ถ๋์ด ๋งค์ฐ ๋ง๊ณ ๋ณต์ก, ๋ด๋ถ๊ตฌ์กฐ ๋ค ํ์ ํ๋ ๊ฒ์ ๋ฌด๋ฆฌ
2. ์คํ๋ง MVC๋ ์๋ง์ ๊ธฐ๋ฅ๋ค์ด ๊ตฌํ๋์ด ์์ด ํด๋น ๊ธฐ๋ฅ์ ํ์ฅํ๊ฑฐ๋ ์ปค์คํ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ง๋ค ์ผ์ ์์.
3. ๊ทธ๋๋ ํต์ฌ๋์ ๋ฐฉ์์ ์์๋๋ฉด ๋๋ฒ๊น ํ๊ฑฐ๋ ํ์ฅ ํฌ์ธํธ๊ฐ ํ์ํ ๋ ์ด๋ค ๋ถ๋ถ์ ํ์ฅํด์ผ ํ๋์ง ๊ฐ์ ์ก์ ์ ์์
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-mvc-1/dashboard
https://catsbi.oopy.io/f52511f3-1455-4a01-b8b7-f10875895d5b
'๐ Backend > MVC Pattern' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring MVC - @RequestMapping, @RequestParam, ModelAndView (0) | 2023.03.09 |
---|---|
๋ค์ํ ๋ฐฉ์์ HandlerMapping, HandlerAdapter, ViewResolver (0) | 2023.03.03 |
Adapter ํจํด (0) | 2023.03.02 |
Front-Controller ํจํด (0) | 2023.02.27 |
Spring / MVC ํจํด (0) | 2023.02.24 |
๋ธ๋ก๊ทธ์ ์ ๋ณด
Study Repository
rlaehddnd0422