Kotlin으로 Spring Boot 개발하기 - mustache 템플릿 설정

Yuri Lee·2020년 9월 21일
0

웹 브라우저에서 "Hello World"출력해보기

  • demo 스프링부트 프로젝트 run
  • 스프링 부트와 http 동작 이해
  • Controller 생성, method와 url 매핑
  • 템플릿 엔진 Mustache 사용
  • 웹브라우저에서 Hello World 확인
  • 고급 url 매핑

Controller 생성, method와 url 매핑

  • HtmlController.kt, Controller 클래스 생성
  • @Controller 어노테이션 부여
  • 클래스 안에 hello world 출력할 수 있는 method 코딩
  • method 에 @RequestMapping(...) 어노테이션 부여

@Controller 어노테이션을 통해 스프링 부트와 연결이 되었음.

localhost:8080 = localhost:8080/

url 매핑은 .. 이걸로만 웹이 클라이언트가 어떤 동작을 요청했는지 알 수 있도록 한다.

funcional 메서드가 동작하려면 해당되는 url이 있어야 한다. 그럼 그 url을 매핑해주는 어노테이션이 RequestMapping이다.

템플릿 엔진 Mustache 사용

  • spring.mustache.suffix=.html
    = templates 폴더에 index.html 생성

템플릿 엔진이란? Mustache 말고도 여러가지가 있다.
spring.mustache.suffix=.html 라고 설정해주자.
그리고 templetes 폴더 안에 index.html파일을 만들자.

package com.example.demo

import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping

@Controller
class HtmlController {

    @RequestMapping("/")
    fun index(model : Model) : String {
        return "index"
    }
}

그러면 mustache가 자동으로 index를 .html 파일로 인식해준다.


[Reference]

이 글은 유투버 kwangsung choi의 코틀린 스프링부트 강좌를 바탕으로 정리한 내용입니다.

profile
Step by step goes a long way ✨

0개의 댓글