jae_coding

[Spring Project] View 환경설정 본문

Spring, java/Spring_Project

[Spring Project] View 환경설정

재코딩 2022. 8. 20. 20:29
반응형

목차

  • Thymeleaf
  • Static Page
  • Template Page

1. Thymeleaf 템플릿 엔진 사용

Thymeleaf의 장점: 웹 브라우저에서 열린다. (다른 템플릿은 하지 못한다!)

 

2. Static Page

resources > static > index.html 생성

<!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>
Hello
<a href="/hello">hello</a>
</body>
</html>

실행화면 (localhost:8080)

Controller를 거쳤을 때, Getmapping이 되지 않았을 때, Static의 index.html실행

3. Template Page

HelloController class

package jpabook.jpashop;

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

// for Thymeleaf check
@Controller
public class HelloController {

    @GetMapping("hello") // (/hello) url으로 들어오면 hello.html이 호출된다.
    public String hello(Model model){
        model.addAttribute("data", "hello!");
        return "hello"; //hello.html 호출
    }
}

Templates > hello.html

<!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>

 

실행화면 (localhost:8080/hello)

 

 

이런식으로 Thymeleaf 엔진을 이용하여 데이터를 넘겨 html을 호출하는 방법을 알아봤습니다.

반응형
Comments