반응형
[Springboot] mustache template 설정
환경 : Springboot v2.1.7, intellij 2021.1.1
mustache 플러그인 설치
build.gradle
의존성 주입
compile('org.springframework.boot:spring-boot-starter-mustache')
mustache의 파일 위치는 기본적으로
src/main/resources/templates
여기에 mustache 파일을 두면 스프링 부트에서 자동으로 로딩한다.
index.mustache
<!DOCTYPE HTML>
<html>
<head>
<title>스프링 부트 웹서비스</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>스프링 부트로 시작하는 웹 서비스</h1>
</body>
</html>
index.mustache index url을 매핑한다
IndexController.java
package com.sooki.book.springboot.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("/")
public String index() {
return "index";
}
}
머스테치 스타터 덕분에 return "index";이라고만 적어줘도
경로(앞)와 파일 확장자(뒤)에 자동으로 붙는다
앞에는 src/main/resources/templates
뒤에는 .mustache
위의 소스에서는 index를 반환하므로
src/main/resources/templates/index.mustache로 전환되어 View Resolver가 처리하게 된다
위의 구성까지해놓고 실행하면 아래와 같이 출력이 된다
IndexControllerTest.java
테스트 코드 작성
package com.sooki.book.springboot.web;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class IndexControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void 메인페이지_로딩() {
// when
String body = this.restTemplate.getForObject("/", String.class);
// then
assertThat(body).contains("스프링 부트로 시작하는 웹 서비스");
}
}
테스트 결과 확인
> [Springboot] mustache template 설정
[Springboot] mustache template으로 게시판 만들기 - 1 (등록)
[Springboot] mustache template으로 게시판 만들기 - 2 (조회)
[Springboot] mustache template으로 게시판 만들기 - 3 (수정)
[Springboot] mustache template으로 게시판 만들기 - 4 (삭제)
Reference
책 스프링 부트와 AWS로 혼자 구현하는 웹 서비스 (이동욱 지음)
반응형