본문 바로가기
Java/스프링 입문 - 스프링 부트, 웹 MVC, DB 접근 기술

[프로젝트 환경설정 - (3)] View 환경설정

by Rezal 2022. 6. 29.

 

https://spring.io 접속 > project > Feature > doc(위는 2.3.1)

7.1.6 의 Welcome Page 설명을 보면, 서버가 실행되고 루트로 접속하면 먼저 resources/static 경로의 index.html 을 먼저 찾고, 없으면 resources/templates 경로의 index 탬플릿을 찾는다고 한다.

 

https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features

 

Core Features

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use a variety of external configuration sources, include Java properties files, YAML files, environment variables, an

docs.spring.io

 

이는 서버에서  파일을 직접 브라우저에 넘겨주는 개념이므로, 프로그래밍이라고 보기 어렵다. 따라서 Thymeleaf 엔진을 이용하여 템플릿을 불러오는 과정을 살펴보자.

 

참고 : Template Engines 관련 설명

 

 

HelloController, templates > hello.html 코드

 

***동작원리

  1. 브라우저에서 localhost:8080/hello url의 GET 방식으로 요청
  2. 스프링부트에 내장되어있는 Tomcat이 스프링에게 물어봄
  3. 스프링에서 해당 url 에 매핑된 Controller와(@Controller) 요청방식(@GetMapping)를 찾고 해당하는 메소드 실행(public String hello(Model model)) - 여기서 Model model 에서 스프링이 Model 을 만들어서 넣어줌
  4. 메소드 실행의 결과(return 'hello')에 해당하는 이름의 파일을 templates 경로에서 찾는다.(hello.html)
  5. viewResolver(클래스의 일종으로 부트에서 로딩되도록 기본 설정됨)가 찾은 화면 이름을 viewName에 매핑하고, Thymeleaf 엔진으로 처리한다(`resources:templates/` + {ViewName}+`.html`)
  6. controller에서 addAttribute로 model(data:hello!!) 를 넘겨주었으므로, hello.html 의 ${data} 에서 data 라는 key의 value를 화면에서 보여줌을 확인할 수 있음