본문 바로가기
코드로 배우는 스프링 부트 - 인프런

view 환경설정

by step 1 2022. 8. 1.
반응형

기존에는 main 코드를 실행시키면 view 파일이 없기 때문에 에러 메시지가 발생한다.

 

작성방법 - 참조

Web (spring.io)

 

Web

Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and servlet-based web applications. It occurs as part of closing the application context and is performed in the earliest

docs.spring.io

위 화면과 같이 resources 폴더 밑 static 폴더에 index.html 파일을 생성해준다.

 

index.html

<!DOCTYPE HTML>
<html>
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>

 

Thymeleaf 템플릿 엔진 사용법

이유: 동작하고 프로그래밍된 페이지를 사용하기 위해서

공식 사이트: Thymeleaf

 

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

 

Controller 작성

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 경로

작성

Controller의 Model을 통해서 data를 받아온다.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>

결과

 

동작 과정

 

반응형