ํ ํ๋ฆฟ ์์ง์ด๋?
ํ ํ๋ฆฟ ์์๊ณผ ํน์ ๋ฐ์ดํฐ ๋ชจ๋ธ์ ๋ฐ๋ฅธ ์ ๋ ฅ ์๋ฃ๋ฅผ ๊ฒฐํฉํ์ฌ ์ํ๋ ๊ฒฐ๊ณผ ๋ฌธ์๋ฅผ ์ถ๋ ฅํ๋ ์ํํธ์จ์ด(๋๋ ์ปดํฌ๋ํธ)๋ฅผ ๋งํ๋ค.
์ฃผ๋ก View๋ฅผ ๋ง๋ค ๋ ์ฌ์ฉํ๋ค.
๋์ ์ปจํ ์ธ ๋ฅผ ์์ฑํด์ ์ ๊ณตํด์ผํ ๋ ํ ํ๋ฆฟ ์์ง์ ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
Code Generation / Email Template ๋ฑ์ ์ฌ์ฉ๋๋ค.
์คํ๋ง ๋ถํธ๊ฐ ์๋ ์ค์ ์ ์ง์ํ๋ ํ ํ๋ฆฟ ์์ง
FreeMarker
Groovy
Thymeleaf (์ถ์ฒ) ใ ก> ๋น๊ต์ ์ต๊ทผ์ ๋ง๋ค์ด์ง ํ ํ๋ฆฟ ์์ง์ด๋ค.
Mustache
JSP๋ฅผ ๊ถ์ฅํ์ง ์๋ ์ด์
spring boot๊ฐ ์งํฅํ๋ ๋ฐ๋ฅผ ์ถฉ์กฑ์ํค๊ธฐ ๋ชปํ๋ค.
spring boot๋ ๋ ๋ฆฝ์ ์ผ๋ก ์คํ๊ฐ๋ฅํ embeded tomcat์ผ๋ก application์ ์ฝ๊ณ ๋น ๋ฅด๊ฒ ๋ง๋ค์ด ๋ฐฐํฌํ๊ธธ ๋ฐ๋๋ค.
JSP๋ JAR ํจํค์ง ํ ๋๋ ๋์ํ์ง ์๊ณ , WAR ํจํค์ง ํด์ผ ํ๋ค.
์๋ธ๋ฆฟ ์์ง ์ค์ ๊ฐ์ฅ ์ต๊ทผ์ ๋ง๋ค์ด์ง, Jboss ์ง์์์ ๋ง๋ Undertow๋ JSP๋ฅผ ์ง์ํ์ง ์๋๋ค.
๋ํ ์์กด์ฑ์์ ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค.
์ด ๋งํฌ๋ ๊ณต์๋ฌธ์๊ฐ ๋งํ๋ jsp์ ์ ์ฝ์ฌํญ์ด๋ค. #
Thymeleaf ์ฌ์ฉํ๊ธฐ
์์กด์ฑ ์ถ๊ฐ
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@RestController๊ฐ ์๋ @Controller๋ก, ๋ณธ๋ฌธ์ ๋ด์ฉ์ด ์๋ ๋ทฐ ์ด๋ฆ์ ์ ๋ฌํ๋ค.
package com.jueun.springmvcdemo;
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", "jueun");// model์ Map์ฒ๋ผ ์ฌ์ฉ
return "helloEveryone";
}
}
ํ ํ๋ฆฟ ํ์ผ ์์น: /src/main/resources/template/helloEveryone.html
xmlns:th="http://www.thymeleaf.org : thymeleaf namespace๋ฅผ ์ถ๊ฐํ๋ค.
<h1 th:text="${name}">Name</h1> : ๋ชจ๋ธ์ name์ ํค๋ก ์ ์ฅํด๋ ๋ด์ฉ์ ๊บผ๋ด ์ด๋ค.
<!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>
Test ํด๋ณด๊ธฐ
์์ฒญ "/hello"
์๋ต
- model์ ๋ด์ ๋ด์ฉ: name : jueun
- view ์ด๋ฆ : helloEveryone
@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()) // ๋ ๋๋ง ๋ณธ๋ฌธ ํ์ธ ๊ฐ๋ฅ
// ํ์๋ฆฌํ ์์ง์ ์๋ธ๋ฆฟ ์ปจํ
์ด๋์ ๋
๋ฆฝ์ ์ธ ์์ง์ด๊ธฐ์ view์ ๋ ๋๋ง ๊ฒฐ๊ณผ๊ฐ๋ ํ์ธํ ์ ์๋ค.
.andExpect(view().name("helloEveryone"))
.andExpect(model().attribute("name", "jueun"))
.andExpect(content().string(containsString("jueun")));
}
}
.andDo(print()): print() ํจ์๋ฅผ ํตํด ๋ ๋๋ง ๋ณธ๋ฌธ์ ํ์ธํ ์ ์๋ค.
์๋ธ๋ฆฟ ์์ง์ด ๊ฐ์ ์ ํด์ผ๋ง ์๋ต์ผ๋ก ๋ด๋ณด๋ผ ์ต์ข ์ ์ธ view๋ฅผ ํ์ธํ ์ ์๋ค. ์ฆ ๋ ๋๋งํ๋ค.
ํ์๋ฆฌํ ์์ง์ ์๋ธ๋ฆฟ ์ปจํ ์ด๋์ ๋ ๋ฆฝ์ ์ธ ์์ง์ ๊ฐ์ง๊ณ ์๋ค. ๊ทธ๋ ๊ธฐ์ view์ ๋ ๋๋ง ๊ฒฐ๊ณผ๊ฐ๋ ํ์ธํ ์ ์๋ค๊ณ ํ๋ค.
์๋๋ hello() ํ ์คํธ๋ฅผ ํต๊ณผํ๊ณ ๊ธฐ๋ก๋ ๋ด์ฉ์ด๋ค.
Body ์ค <h1 th:text="${name}">Name</h1>์์ ๊ฐ์ด jueun์ผ๋ก ์ ๋ ๋๋ง๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
MockHttpServletRequest:
HTTP Method = GET
Request URI = /hello
Parameters = {}
Headers = []
Body = null
Session Attrs = {}
Handler:
Type = com.jueun.springmvcdemo.SampleController
Method = com.jueun.springmvcdemo.SampleController#hello(Model)
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = helloEveryone
View = null
Attribute = name
value = jueun
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Content-Language:"en", Content-Type:"text/html;charset=UTF-8"]
Content type = text/html;charset=UTF-8
Body = <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h1>jueun</h1>
</body>
</html>
Forwarded URL = null
Redirected URL = null
Cookies = []
Thymeleaf ๊ณต์ ํํ์ด์ง #
Thymeleaf 5๋ถ ํํ ๋ฆฌ์ผ #
Thymeleaf ์์ #
'Springboot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring boot] HATEOAS ๊ฐ๋จํ๊ฒ ์ฌ์ฉํด๋ณด๊ธฐ (0) | 2021.01.17 |
---|---|
[spring boot] index.html, favicon (0) | 2021.01.10 |
[spring boot] WebJars ์ค jquery ์ถ๊ฐํด๋ณด๊ธฐ (0) | 2021.01.10 |
[spring boot] ์ ์ ๋ฆฌ์์ค ์ง์ (0) | 2021.01.10 |
[spring boot] HttpMessageConverters (0) | 2021.01.10 |
[spring boot] Test (0) | 2021.01.09 |