Deep Dive /Java Web /17-V6

17-V6. ThymeleafView → TemplateEngine: process()

ThymeleafView는 모델과 요청 정보를 모은 뒤, 최종적으로 TemplateEngine.process(...)를 호출해 템플릿을 HTML로 변환합니다.


이번 단계의 역할 — 뷰 객체 안에서 템플릿 엔진 실행이 시작된다

뷰 객체 내부에서 실제 템플릿 엔진 실행이 시작되는 핵심 단계입니다.


호출 흐름 요약 — mergedModel·WebExpressionContext 준비 → content type·writer 세팅 → process()

  1. ThymeleafView가 mergedModelWebExpressionContext를 준비합니다.
  2. 응답 content type과 writer를 세팅합니다.
  3. viewTemplateEngine.process(...)를 호출합니다.
  4. 템플릿 엔진이 HTML 조각을 writer에 씁니다.

호출 흐름 다이어그램 — ThymeleafView가 TemplateEngine을 불러 writer에 HTML을 낸다

sequenceDiagram participant View as ThymeleafView participant Engine as TemplateEngine participant Writer View->>Engine: process(templateName, context, writer) Engine-->>Writer: HTML output

핵심 코드 — FastStringWriter를 만들어 viewTemplateEngine.process()에 넘긴다

// ThymeleafView.java
final Writer templateWriter =
        (producePartialOutputWhileProcessing ? response.getWriter() : new FastStringWriter(1024));

viewTemplateEngine.process(templateName, processMarkupSelectors, context, templateWriter);
// TemplateEngine.java
templateEngine.process("mytemplate", ctx, httpServletResponse.getWriter());

코드 해설 — 모델·로케일·표현식 컨텍스트·writer까지 준비한 뒤에야 엔진을 부른다

ThymeleafView는 단순히 템플릿 이름만 넘기는 것이 아니라, 모델·로케일·표현식 컨텍스트·writer까지 모두 준비한 뒤 엔진을 호출합니다. 그래서 템플릿 엔진은 웹 요청 문맥을 이해한 상태에서 렌더링할 수 있습니다.


설계 의도 — View가 웹 문맥을 조립하고 템플릿 엔진은 렌더링만 담당한다

View 계층에서 웹 문맥을 조립하고, 템플릿 엔진은 렌더링만 담당하게 하려는 분리 구조입니다.


다음 단계 연결 — 17-V7 HTML이 DispatcherServlet 흐름으로 복귀하는 과정으로 이어진다

다음 문서 17-V7에서는 템플릿 엔진이 만든 HTML이 어떻게 다시 DispatcherServlet 흐름으로 복귀하는지 봅니다.

← 이전: 17-V5. DispatcherServlet → ThymeleafView | 다음: 17-V7. ThymeleafView → DispatcherServlet: HTML 반환