article thumbnail image
Published 2022. 11. 18. 16:28

 

 

라이브러리 추가

스프링부트 버전과 동일한 머스테치 라이브러리 의존성을 등록해준다.

 

implementation 'org.springframework.boot:spring-boot-starter-mustache:2.7.5'

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mustache/2.7.5

 

 

설정파일 생성

현재 머스테치는 .mustache 파일을 인식하게 되어있다.

 

머스테치가 .html 파일을 인식할 수 있도록 별도의 설정파일을 만들어준다.

 

package com.example.mustache.config;

import org.springframework.boot.web.servlet.view.MustacheViewResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MustacheConfig implements WebMvcConfigurer {

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		MustacheViewResolver resolver = new MustacheViewResolver();
		resolver.setCharset("UTF-8");
		resolver.setContentType("text/html; charset=UTF-8");
		resolver.setPrefix("classpath:/templates/");
		resolver.setSuffix(".html");

		registry.viewResolver(resolver);
	}
}

WebMvcConfigurer를 impl해서 위 메서드를 구현해주면 된다.

 

UTF-8, html, 경로 등을 설정해주면 이제 머스테치 템플렛이 .html파일을 인식할 수 있다.

복사했습니다!