๐ŸŒ WEB/Thymeleaf

Thymeleaf ์‚ฌ์šฉํ•˜๊ธฐ

์• ์ •์“ฐ 2021. 3. 2. 21:21

jsp๋งŒ ๋ฐฐ์šฐ๊ณ  ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ต์ˆ™ํ•ด์ง„ ๊ฒƒ์„ ๊ณ„์† ์“ฐ๊ณ  ์‹ถ์—ˆ์ง€๋งŒ Spring Boot๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด์„œ Thymeleaf๋ฅผ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์šฐ์„ ์ ์œผ๋กœ Spring boot์—์„œ jsp ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์•„๋งˆ ๋‚ด์žฅํ†ฐ์บฃ์ด๊ธฐ ๋•Œ๋ฌธ์— war๋กœ ํŒจํ‚ค์ง•์„ ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Spring boot๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š” ์ž…์žฅ์—์„œ๋Š” ์•ฝ๊ฐ„ ๐Ÿคจ์ž…๋‹ˆ๋‹ค.

 

Thymeleaf๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์˜์กด์„ฑ์„ ๋จผ์ € ์ถ”๊ฐ€ ํ•ด์ค๋‹ˆ๋‹ค.

 

build.gradle

    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

 

application.yml์—๋„ ๊ด€๋ จ ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. jsp๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ฒ˜๋Ÿผ ์œ„์น˜์™€ ํŒŒ์ผํ˜•์‹์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค

application.yml

spring:
	thymeleaf:
	    prefix: classpath:templates/
	    suffix: .html
	    mode: HTML5

 

 

ํŒจํ‚ค์ง€ ๊ตฌ์กฐ๋Š” ์ด๋ ‡์Šต๋‹ˆ๋‹ค.

 

template ํ˜•ํƒœ๋Š” ์ด๋ ‡์Šต๋‹ˆ๋‹ค. ์œ„์— xmlns:th ๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.!

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Dealink Admin Page</title>
</head>
<body>
</body>
</html>

Controller

  @RequestMapping("/room")
  public String room() {
    return "room";
  }

 

Test Code (ํ…Œ์ŠคํŠธ ์ฝ”๋“œ JUnit5)

package com.chatting.domains.chatting.presentation;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

@ExtendWith(SpringExtension.class)
@WebMvcTest(controllers = ChattingController.class)
class ChattingControllerTest {

  @Autowired private MockMvc mockMvc;
  @Autowired WebApplicationContext wac;

  @BeforeEach
  public void setup() {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
  }

  @Test
  public void html๋งตํ•‘ํ…Œ์ŠคํŠธ() throws Exception {
    this.mockMvc
        .perform(MockMvcRequestBuilders.get("/room"))
        .andExpect(status().isOk())
        .andDo(print())
        .andExpect(view().name("room"));
  }
}

๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ• ๋ช‡๊ฐœ๋กœ ์˜ˆ๋ฅผ ๋“ค์–ด๋ณด๋ฉด

Text

์ปจํŠธ๋กค๋Ÿฌ์—์„œ model๋กœ ๋ฐ›์•„์˜จ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ธํ…”๋ฆฌ์ œ์ด์—์„œ ์ž๋™์™„์„ฑ์„ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋–ค ๊ฐ’์„ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ๋Š”์ง€ ์‰ฝ๊ฒŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค !

<p th:text="'์ด์ž…์ฐฐ ์ˆ˜: '+${countBidHistory}"></p>

 

๋ฐ˜๋ณต๋ฌธ

auctionResponseList๋ผ๋Š” ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด๋ฅผ ๋ฐ›์•„ ์™”์„ ๋•Œ ์ฒ˜๋ฆฌ ์ด๋ ‡๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<tr th:each="auction : ${auctionResponseList}">
	<td th:text="${auction.date}">์ƒํ’ˆ ๋“ฑ๋ก ๋‚ ์งœ</td>
</tr>

 

aํƒœ๊ทธ

 

"" ์•ˆ์— ''๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์ •์ƒ์ ์œผ๋กœ ๋ฐ˜์˜์ด ๋ฉ๋‹ˆ๋‹ค! ์•ž์— @๋ฅผ ๋ถ™์ด๋Š”๊ฒƒ์ด ํ•ต์‹ฌ์ž…๋‹ˆ๋‹ค.

<a th:href="@{'https://www.aejeong.co.kr/product/'+${auction.auctionEntity.url}}"
               th:target="_blank">์ƒํ’ˆํŽ˜์ด์ง€ ๋ณด๊ธฐ</a>

 

onClick

<a th:onclick="'window.open(\'/auction/history/'+${auction.auctionEntity.id}+'\',\'width=700px,height=800px,scrollbars=yes\')'"
               th:text="${auction.count}" style="color: blue;text-decoration: underline">๊ฒฝ๋งค ๋‚ด์—ญ</a>

 

format

๋ฐ์ดํŠธ ํฌ๋งท๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค! #temporals.format์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

<td th:text="${#temporals.format(auction.historyEntity.createdDate,'yyyy-MM-dd HH:mm')}"></td>
๋ฐ˜์‘ํ˜•