spring

[mustache] 의존성 추가, IndexController

2022. 5. 17. 17:19
목차
  1. 머스테치 의존성 추가
  2. IndexController
  3. IndexControllerTest
  • 루비, js, 파이썬, PHP, java, 펄, GO, ASP 등 대부분의 언어 지원하는 템플릿 엔진
  • 자바에서는 서버 템플릿 엔진, js에서는 클라이언트 템플릿 엔진으로 사용 가능

 

머스테치 의존성 추가

build.gradle에 등록

compile('org.springframework.boot:spring-boot-starter-mustache')

파일 위치는 기본적으로 src/main/resources/templates

 

IndexController

package com.springAWS.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController { //페이지에 관련된 컨트롤러
@GetMapping("/")
public String index() {
return "index";
//머스테치 스타터 덕에 컨트롤러에서 문자열 반환 시 앞의 경로와 뒤의 파일 확장자 자동 지정됨
//src/main/resources/templates/index.mustache로 전환돼 View Resolver 처리하게 됨
}
@GetMapping("/posts/save")
public String postsSave(){
return "posts-save";
}
}
  • 머스테치 스타터 덕분에 컨트롤러에서 문자열 반환할 때 앞의 경로, 파일 확장자 자동 지정됨
  • 페이지 관련된 컨트롤러는 모두 IndexController 사용함

 

IndexControllerTest

package com.springAWS.web;
import junit.framework.TestCase;
import org.assertj.core.api.Assertions;
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 mainPageLoading(){
//when
String body = this.restTemplate.getForObject("/", String.class);
//then
assertThat(body).contains("스프링 부트로 시작하는 웹 서비스");
}
}
  • URL 호출 시 페이지 내용 제대로 호출되는지 확인 위한 테스트
  1. 머스테치 의존성 추가
  2. IndexController
  3. IndexControllerTest
'spring' 카테고리의 다른 글
  • [OAuth] 구글 서비스 등록, properties 설정
  • [mustache] 등록, 조회 화면
  • 템플릿 엔진
  • Application
hahihi
hahihi
hahihi
히호 노트
hahihi
전체
오늘
어제
  • 분류 전체보기 (224) N
    • 알고리즘 (114)
      • 정렬 (3)
      • 그리디 (9)
      • 구현 (35)
      • 이분 탐색 (4)
      • 탐색 (2)
      • 동적 계획법 (DP) (11)
      • DFS BSF (29)
      • 최단 경로 (5)
      • 그래프 (4)
      • 주의할 점 (4)
      • 트리 (3)
    • spring (34)
      • JPA (12)
    • DevOps (10)
      • Docker (3)
    • java (15)
      • 이펙티브자바 (4)
      • Clean Code (4)
    • git (9)
    • DB (3)
    • 앱개발 (1)
    • 유닉스 (26)
    • 네트워크 (3) N
      • IT 엔지니어를 위한 네트워크 입문 (3) N
    • 유니티 (1)
    • 후기 (4) N
    • 누누코코 (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • dp
  • 1868
  • Docker
  • 도넛과 막대 그래프
  • 이코테
  • 숫자 조각
  • BaseEntity
  • 7465
  • JWT
  • 입출력
  • allowPublicKeyRetrieval
  • 13265
  • 공통 response
  • 백준
  • 4193
  • 프로그래머스
  • 그리디 알고리즘
  • 팀 결성
  • SWEA
  • spring

최근 댓글

최근 글

hELLO · Designed By 정상우.
hahihi
[mustache] 의존성 추가, IndexController
상단으로

티스토리툴바

개인정보

  • 티스토리 홈
  • 포럼
  • 로그인

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.