6.DispatcherServlet 동작 과정
1. DispatcherServlet의 세부 동작 과정(1-3.)
이전 1-3. DispatcherServlet은 사용자의 요청을 처리하기 위한 작업 수행 과정에 대한 세부 과정은 다음과 같음

DispatcherServlet은 HandlerMapping을 통해 사용자의 요청 URL에 해당하는 요청 처리를 수행할 개별 컨트롤러가 누구인지 조회
→ 2.Find a Controller mapped by request URL
HandlerMapping은 해당하는 요청을 수행할 컨트롤러의 정보를 DispatcherServlet에게 반환
→ 3. return mapped controller information
DispatcherServlet은 HandlerMapping에게 받은 정보를 기반으로 HandlerAdapter에게 요청을 수행하도록 위임(Delegate)
→ 4. Delegate executing the controller to HandlerAdapter
HandlerAdapter는 실제 컨트롤러를 호출
→ 5. Execute a Controller
특정 Controller.java는 Service → DAO를 거쳐서 전달받은 요청을 수행, DB의 결과값을 반환 받음
→ 6. Execute a Service ~ 11. Return data
Controller.java는 전달받은 DB의 결과값 및 리다이렉트할 URL 정보를 ModelAndView 객체에 바인딩
HelloController.java
package com.example.demo.controller;
import com.example.demo.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
private final HelloService helloService;
@Autowired
public HelloController(HelloService helloService) {
this.helloService = helloService;
}
@GetMapping("/hello")
public ModelAndView sayHello() {
// 서비스 계층을 통해 DB에서 결과 값을 가져옴
String result = helloService.getHelloMessage();
// ModelAndView 객체 생성
ModelAndView modelAndView = new ModelAndView();
// 결과 값을 모델에 추가
modelAndView.addObject("message", result);
// 뷰 이름 설정 (DispatcherServlet이 /WEB-INF/views/list.jsp로 포워딩)
modelAndView.setViewName("list");
return modelAndView;
}
}
Controller.java(여기서는 HelloController.java)는 ModelAndView 객체를 HandlerAdapter에게 반환
→ 12. Return the ModelAndView
HandlerAdapter는 자신의 작업 처리 후 DispatcherServlet에게 ModelAndView를 반환
→ 13. Return the ModelAndView
DispatcherServlet은 ViewResolver를 통해 처리할 View 페이지의 경로 확인
→ 14. Find a Logical View Name ~ 15. Physical Location of View
Model 객체를 실제 View 파일에 전달하여 사용자에게 페이지를 렌더링
→ 16. Return Model ~ 18. Response to the Client
Last updated on