IT/Java - Spring Boot

[Spring Boot] thymeleaf 의존성 추가하기 (Controller가 templates의 html을 인식하지 못할 때)

ITyranno 2024. 1. 19. 11:47
728x90
반응형

 

 

 

 

 

 

 

 

프로그래밍 세계를 탐구합시다.

 

 

 

 

 

 

 

thymeleaf 의존성 추가하기

 

 

 

templates의 html파일을 controller가 읽어오기 위해서는 Thymeleaf 의존성을 추가해야 합니다.

 

 

 

 

Maven 의존성 추가하기

 

pom.xml

 

아래의 코드를 pom.xml 에 추가합니다.

 

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

 

 

 

 

FunctionController.java

경로 :  src>main>java>com>example>turtlehunter>controller>FunctionController.java

package com.example.turtlehunter.controller;

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

@Controller
public class FunctionController {
    @GetMapping({ "/function" })
    public String function(Model model) {
        model.addAttribute("data", "hello!!");
        return "function";
    }
}

 

 

 

function.html

 경로 : src>main>resources>templates>function.html

<body>
    <h1>안녕</h1>
</body>

 

 

 

 

 

Gradle 의존성 추가하기

 

 

build.gradle

 

 

아래의 코드를 build.gradle 에 추가합니다.

 

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect')
}

 

 

 

 

 

 

위와 같이 설정하면 templates의 html을 Controller가 인식할 수 있습니다.

 

 

728x90
반응형