일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- 디자인패턴 #싱글톤
- tomcat
- autocomplete
- 외장톰캣
- Spring Framework
- 톰캣
- spring
- LiveTemplate
- Mockito #Reflection #Sigleton #Test #JUnit
- Today
- Total
자라선
19. Thymeleaf 본문
https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html
스프링 부트에서 공식적으로 지원하는 템플릿 엔진
무조건 템플릿 엔진을 사용하여 랜더링 후 결과를 출력하는 다른 템플릿 엔진과는 다르게
타임리프는 그 즉시 결과 화면을 출력할 수있어 디자이너와 협업하기에 적합하다는 장점이 있다.(타임리프에서 강조하는 장점..)
템플릿 엔진은 view만 생성하는것이 아닌 code generation이나 이메일 템플릿에 작성하는데 등등 사용한다.
정적인 html을 동적으로 사용할 수 있도록 랜더링하여 생성해준다.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
레이아웃 템플릿 엔진
중복되는 코드들을 include를 사용하지 않고 정해진 레이아웃에 따라 페이지들을 조합하여 사용하는 엔진
예) Apache Tiles, Stiemesh
텍스트 템플릿 엔진
정해진 양식에 맞춰 템플릿을 랜더링 후 문서로 출력 하는 엔진
예) JSP, Freemarker, thymeleaf 등
스프링 부트에서 자동 설정을 지원 해주는 템플릿 엔진
· Freemarker
· Groovy
· thymeleaf
· mustache
스프링부트에서는 JSP를 권장하지 않음
JSP는 스프링 부트가 지향하는 방향과 틀리며, jar 패키징 할때 동작하지 않는다.
스프링부트는 임베디드 톰캣을 사용하여 손쉽개 개발 배포를 하는것을 목표로 하지만 JSP는 jar 패키징이 되질 않으며 JSP의 의존성을 정의하면
스프링부트 내부에서의 의존성 이슈가 발생할 수 있기 때문이고 Undertow was에서는 JSP 를 지원하지 않는다.
또한 사용자 정의 error.jsp로 작성해도 스프링 부트는 기본 에러페이지를 받아주지않음
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jsp-limitations
타임리프 테스트
타임리프는 서블릿 컨테이너에 독립적인 템플릿 엔진이기 때문에 바로 렌더링된 view 를 테스트에서 쉽게 확인할 수 있다.
JSP는 서블릿 컨테이너에 종속적이기 때문에 mockMvc 객체를 주입받아서 테스트는 불가능하다.
mockMvc 객체는 서블릿 컨테이너를 가상으로 구현하는 것이므로 본래의 서블릿 컨테이너의 역할을 수행하지 못하기 때문
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@WebMvcTest(SampleController.class)
public class SampleControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void hello() throws Exception {
mockMvc.perform(get("/hello"))
.andExpect(status().isOk())
.andDo(print())
.andExpect(view().name("hello"))
.andExpect(model().attribute("name", is("thkong")))
.andExpect(content().string(containsString("thkong"))); // 랜더링 된 view에서 thkong 문자열이 포함되었는지 테스트
}
}
html 문서에 네임스페이스를 작성 해야한다.
문서 속성에 타임리프 문법을 추가하고 attribute로 들어온 값을 넣어준다. 값이 없다면 해당 태그의 Value 값을 보여줌
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h1 th:text="${name}">Name</h1>
</body>
</html>
Controller를 생성하지 않고 간단하게 WelcomePage 생성 방법(웰컴페이지)
컨트롤러 생성하지 않고 간편 핸들링
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/hello").setViewName("hello");
}
}
'Develop > Spring Boot' 카테고리의 다른 글
21. ExceptionHandler (0) | 2020.07.27 |
---|---|
20. htmlUnit (0) | 2020.07.27 |
18. webJars (0) | 2020.07.27 |
16. Spring MVC (0) | 2020.07.27 |
15. devtools (0) | 2020.07.27 |