[Spring Boot #14] 스프링 웹 MVC - Thymeleaf
- 📚 Spring/Spring Boot
- 2020. 5. 4. 09:24
스프링 부트가 자동설정을 지원하는 템플릿 엔진
- FreeMarker
- Groovy
- Thymeleaf
- Mustache
Jsp 권장하지 않는 이유
- Jar 패키징 할때는 동작하지 않고, war로 패미징을 해야한다.
- Undertow는 JSP를 지원하지 않는다. (JBOSS에서 지원하는 서블릿 엔진이다)
- jsp는 서블릿엔진이 템플릿을 완성시킨다.
학습 및 참고 URL
https://www.thymeleaf.org/doc/articles/standarddialect5minutes.html
| Thymeleaf란?
- Thymeleaf는 스프링 부트가 자동설정을 지원하는 웹 템플릿 엔진이다. HTML문서에 HTML5 문법으로 서버쪽 로직을 수행하고 적용시킬 수 있다.
- HTML디자인에 전혀 영향을 미치지 않고 웹 템플릿 엔진을 통해 HTML을 생성할 수 있다.
- 스프링부트는 jsp를 권장하지 않는다.
| Thymeleaf 스프링 부트에서 사용하기
테스트 코드
package com.kyhslam.spring;
import org.hamcrest.Matchers;
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 org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import 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 {
// 요청 "/hello"
// 응답
// - 모델 name : kyhslam
// - 뷰 이름 : hello
mockMvc.perform(MockMvcRequestBuilders.get("/hello"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print()) // 렌더링 결과 확인가능
.andExpect(MockMvcResultMatchers.view().name("hello"))
.andExpect(MockMvcResultMatchers.model().attribute("name", Matchers.is("kyhslam")))
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("kyhslam")));
}
}
소스코드
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.kyhslam.spring;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class SampleController {
@GetMapping("/hello")
public String hello(Model model){
model.addAttribute("name","kyhslam");
return "hello";
}
}
- Model 객체에 속성값 name : kyhslam 을 추가 후, 반환한다.
- 해당 Model 객체에 포함된 값을 통해 Thymeleaf 템플릿 엔진이 해당 템플릿에 명시한 값을 반환한다.
resources-templates-hello.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${name}">Name</h1>
</body>
</html>
- xmlns:th="http://www.thymeleaf.org" 를 명시해야 템플릿이 제대로 렌더링된다.
- th:text="${name}" 에서 Model에서 넘어온 값을 변환한다.
'📚 Spring > Spring Boot' 카테고리의 다른 글
[Spring Boot #16] 스프링 ExceptionHandler (0) | 2020.05.14 |
---|---|
[Spring Boot #15] 스프링 웹 MVC - HtmlUnit 써보기 (0) | 2020.05.04 |
[Spring Boot #13] 스프링 웹 MVC - index페이지와 파비콘 (0) | 2020.05.04 |
[Spring Boot #12] 스프링 웹 MVC - 정적 리소스 (0) | 2020.05.03 |
[Spring Boot #11] 스프링 웹 MVC - HttpMessageConverters (0) | 2020.05.03 |