๐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ผ์ข ์ผ๋ก, HTML์ ๋ด์ฉ๋ฌผ์ ๋์ ์ผ๋ก ๋ณ๊ฒฝํ๋๋ฐ ์ฃผ๋ก ์ฌ์ฉ๋จ
โ ์ฑ์์ ธ์ผ ํ๋ ๋ถ๋ถ์ด ์กด์ฌํ๋ HTML์ ํ ํ๋ฆฟ์ผ๋ก ํ์ฉ, ์ฑ์์ผํ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณต๋ฐ์ผ๋ฉด ๊ทธ ๋ถ๋ถ์ ๋ฐ์ดํฐ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ฑ์๋ฃ์ ๋ค, ์์ฑ๋ HTML์ ๋๋ ค์ค
๐ ์๋ฒ ์ฌ์ด๋ Java ํ ํ๋ฆฟ ์์ง์ผ๋ก, HTML, XML, JavaScript, CSS ๋ฑ์ ๋งํฌ์ ๋ฌธ์๋ฅผ ์์ฑํ๊ณ ๋ ๋๋งํ๋ ๋ฐ ์ฌ์ฉ๋จ
๐ ์ผ๋ฐ์ ์ผ๋ก Spring ํ๋ ์์ํฌ์ ํตํฉ๋์ด ๋ง์ด ์ฌ์ฉ๋๋ฉฐ, Spring MVC์ ํจ๊ป ์ฃผ๋ก ์ฌ์ฉ๋๋ ํ ํ๋ฆฟ ์์ง ์ค ํ๋
๐ง start.spring.io๋ฅผ ์ด์ฉํ๋ค๋ฉด Dependencies์์ Thymeleaf๋ฅผ ์ถ๊ฐ
๐ง Gradle๊ณผ Maven์ ๋ฐ๋ผ ThymeleafViewResolver๋ฅผ ์ถ๊ฐ
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
๐ ์ ์ค์ ์ถ๊ฐ ํ ๋น๋ํ๋ฉด Thymeleaf๋ฅผ ์ด์ฉํ ์ ์์
๋๋ถ๋ถ์ html ์์ฑ์ th:xxx
๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
ํํ | ์ค๋ช | ์์ |
---|---|---|
@{ ... } | URL ๋งํฌ ํํ์ | th:href="@{/css/bootstrap.min.css}" th:href="@{/{itemId}/edit(itemId=${item.id})}" |
| ... | | ๋ฆฌํฐ๋ด ๋์ฒด | th:text="|Hi ${user.name}!|" ( = th:text="'Hi '+${user.name}+'!'" ) |
${ ... } | ๋ณ์ | th:text=${user.name} |
[[${ โฆ }]] | ๋ณ์ ์ง์ ์ถ๋ ฅ | <tr th:each="item: ${items}"> โ<td th:text="${item.price}">100</td> </tr> |
*{ ... } | ์ ํ ๋ณ์ | <tr th:object="${items}"> โ<td th:text="*{price}">100</td> </tr> |
#{ ... } | properties๊ฐ์ ์ธ๋ถ ์์์์ ์ฝ๋์ ํด๋นํ๋ ๋ฌธ์์ด get | th:text="#{member.register}" |
@Controller
public class MvcController {
private int hitCount = 0;
@RequestMapping("/hits")
public String hits(Model model) {
model.addAttribute("hits", ++hitCount);
return "hits";
}
}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Hits</title>
</head>
<body>
<!-- ์๋ก๊ณ ์นจํ ๋๋ง๋ค hits๊ฐ ์ฆ๊ฐํ๋ค. -->
<p>Today's Hits: [[${hits}]]</p>
</body>
</html>
@Controller
public class MvcController {
@RequestMapping("/lotto")
public String lotto(Model model) {
List<Integer> listOfNums = new ArrayList<>();
// ๋๋ค ๋ฐ์์ 6๋ฒ ์ถ๋ ฅ โ listOfNums์ ๋ด๊ธฐ
for (int i = 0; i < 6; i++) {
listOfNums.add((int)(Math.random()*46)); // ์ ์๋ก 0<random<46 ๋ฒ์๋ฅผ ์ ํจ
}
model.addAttribute("listOfNums",listOfNums);
return "lotto";
}
}
<!-- lotto.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Lotto</title>
</head>
<body>
<span th:each="item: ${listOfNums}">[[${item}]] - </span>
<div>
<a href="/hits">๋ค๋ก๊ฐ๊ธฐ</a>
<a href="/lotto">๋ค์ํ๊ธฐ</a>
</div>
</body>
</html>
@Controller
public class MvcController {
// listOfLotto ๋ฌธ์์ด ๋ฐฐ์ด ์์ฑ
private List<String> listOfLotto = new ArrayList<>();
@RequestMapping("/lotto")
public String lotto(Model model) {
// lotto ๋ถ๋ฌ์ฌ๋๋ง๋ค listOfNums ์ด๊ธฐํ
List<Integer> listOfNums = new ArrayList<>();
for (int i = 0; i < 6; i++) {
listOfNums.add((int)(Math.random()*46));
}
listOfLotto.add(listOfNums.toString());
model.addAttribute("listOfNums", listOfNums);
return "lotto";
}
@RequestMapping("/history")
public String history(Model model) {
// ์ฐ์ listOfLotto๊ฐ ์กด์ฌํ๋์ง ์ฒดํฌ
boolean isHistory;
if (!listOfLotto.isEmpty()) isHistory = true;
else isHistory = false;
// ๊ทธํ ์์ฑ ๋๊ฒจ์ฃผ๊ธฐ
model.addAttribute("listOfLotto", listOfLotto);
model.addAttribute("isHistory", isHistory);
return "history";
}
}
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>History</title>
</head>
<body>
<div>
<h2>Lotto List</h2>
<!-- isHistory bool ๊ฐ์ ๋ฐ๋ผ ์ถ๋ ฅ ๊ฒฐ๊ณผ๊ฐ ๋ค๋ฅด๋ค. -->
<div th:if="${isHistory}">
<p th:each="item: ${listOfLotto}">[[${item}]]</p>
</div>
<div th:unless="${isHistory}"><p>์์ง ๊ธฐ๋ก์ด ์์ต๋๋ค.</p></div>
</div>
<a href="/hits">๋์๊ฐ๊ธฐ</a>
</body>
</html>