본문 바로가기
Spring

[스프링 입문] View 환경설정

by 스마일포테이토 2022. 12. 26.

https://spring.io/projects/spring-boot

 

Spring Boot

Commercial support Business support from Spring experts during the OSS timeline, plus extended support after OSS End-Of-Life. Publicly available releases for critical bugfixes and security issues when requested by customers.

spring.io

LEARN -> Reference Doc에서 찾아 들어가 검색

web->index.html검색하면 welcome page 관련 내용 찾을 수 있음

 

스프링 부트가 제공하는 Welcome Page 기능

- static/index.html을 올려두면 welcome page 기능을 제공한다.

 

 

thymeleaf  템플릿 엔진

- thymeleaf 공식 사이트

https://www.thymeleaf.org/

 

Thymeleaf

Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati

www.thymeleaf.org

spring doc에서 template engine 검색하면 관련 내용 확인 가능

 

@GetMapping("hello")

웹 어플리케이션에서 /hello로 들어오면 @GetMapping("hello")가 붙은 메소드를 spring이 호출해준다.

package hello.hellospring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("hello")
    public String hello(Model model){
        model.addAttribute("data", "hello!!");
        return "hello";
    }
}

 

 

hello.html에는 thymeleaf 템플릿 엔진 선언, th로 선언하고 사용

<!DOCTYPE html>
<html xmlns:th="http://www.thymleaf.org">
<p th:text="'안녕하세요.'+ ${data}">안녕하세요. 손님</p>

thymeleaf 문법 사용해서 model.addAttribute에서 data의 이름으로 hello!!값을 가지고 있는 모델으로 치환

localhost:8080에서 hello 클릭하면 안녕하세요 hello!! 확인 가능하다.

 

웹 브라우저에서 /hello 던지면 tomcat을 내장하고 있는 스프링 부트가 받음

helloController가 hello 리턴하면 resources의 templates의 밑에있는 hello를 찾아서 렌더링 (기본 설정) 

 

컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버 (viewResolver)가 화면을 찾아서 처리한다.

- 스프링 부트 템플릿엔진 기본 viewName 매핑

- `resources:templates/`+{ViewName}+`.html`

resources에 있는 template의 viewName으로 된 html이 열리게 된다.

 

 

*참고

spring-boot-devtools 라이브러리를 추가하면, `html`파일을 컴파일만