index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>
<h1>Hello Thymeleaf!!</h1>
<p><a href="hithymeleaf">hithymeleaf.html</a></p>
</body>
</html>
ThymeleafController.java
@GetMapping(value="hithymeleaf")
public String hithymeleaf(Model model) {
model.addAttribute("say", "안녕하세요");
return "basic/01_hithymeleaf";
}
01_hithymeleaf.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p><span th:text="${say}">Hi</span> Thymeleaf!!</p>
</body>
</html>
<!-- /* -->
<!-- HTML주석 -->
<!--/* Thymeleaf주석 */-->
<!--/*
두 줄 이상
*/-->
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>
<h1>Hello Thymeleaf!!</h1>
<p><a href="hithymeleaf">hithymeleaf.html</a></p>
<p><a href="literal">literal.html</a></p>
</body>
</html>
ThymeleafController.java
@GetMapping(value="literal")
public String literal(Model model) {
model.addAttribute("data", "스프링부트");
return "basic/literal";
}
literal.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<ul>
<li>'hello' + 'world' = <span th:text="'hello' + 'world'"></span></li>
<li>'hello world' = <span th:text="'hello world'"></span></li>
<li>'hello' + ${data} = <span th:text="'hello' + ${data}"></span></li>
<li>리터럴 대체 |hello' + ${data}| = <span th:text="'hello' + ${data}"></span></li>
</ul>
</body>
</html>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>
<h1>Hello Thymeleaf!!</h1>
<p><a href="hithymeleaf">hithymeleaf.html</a></p>
<p><a href="literal">literal.html</a></p>
<p><a href="operation">operation.html</a></p>
</body>
</html>
ThymeleafController.java
@GetMapping(value="operation")
public String operation(Model model) {
//model.addAttribute("data", "스프링부트");
return "basic/operation";
}
operation.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<ul>
<li>
산술연산자
<ul>
<li>25 + 36 = <span th:text="25+36"></span><li>
<li>25 % 2 == 0 = <span th:text="25 % 2 == 0"></span></li>
</ul>
<ul>
<li>25 + 36 = [[${25+36}]]<li>
<li>25 % 2 == 0 =[[${25%2==0}]]</span></li>
</ul>
</li>
<li>
비교연산자
<ul>
<li>1 > 10 = <span th:text="1 > 10"></span></li>
<li>1 gt 10 = <span th:text="1 gt 10"></span></li>
<li>1 >= 10 = <span th:text="1 >= 10"></span></li>
<li>1 ge 10 = <span th:text="1 ge 10"></span></li>
<li>1 == 10 = <span th:text="1 == 10"></span></li>
<li>1 != 10 = <span th:text="1 != 10"></span></li>
</ul>
</li>
<li>
조건식
<ul>
<li>(10 % 2==0? '짝수' : '홀수') = <span th:text="(10 % 2 == 0) ? '짝수': '홀수'"></span></li>
</ul>
</li>
<li>
Elvis(?:) 연산자 - null 또는 빈 값 일때 사용
<ul>
<li>${data}?: '데이터가 없습니다.' = <span th:text="${data}?: '데이터가 없습니다.' "></span></li>
<li>${nullData}?: '데이터가 없습니다.' = <span th:text="${nullData}?: '데이터가 없습니다.' "></span></li>
<li>${bb}?: '데이터가 없습니다.' = <span th:text="${bb}?: '데이터가 없습니다.' "></span></li>
</ul>
</li>
<li>
No-Operation
<ul>
<li>${data}?: _ = <span th:text= "${data}?: _">데이터가 없습니다.</span></li>
<li>${nullData}?: _ = <span th:text= "${nullData}?: _">데이터가 없습니다.</span></li>
</ul>
</li>
</ul>
</body>
</html>
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h3>
<h1>Hello Thymeleaf!!</h1>
<p><a href="hithymeleaf">hithymeleaf.html</a></p>
<p><a href="literal">literal.html</a></p>
<p><a href="operation">operation.html</a></p>
<p><a href="variable">variable.html</a></p>
</body>
</html>
ThymeleafController.java
@GetMapping(value="variable")
public String variable(Model model) {
PersonDTO aa = new PersonDTO("홍길동", 25);
PersonDTO bb = new PersonDTO("김태리", 32);
PersonDTO cc = new PersonDTO("이제훈", 40);
//리스트
List<PersonDTO> list = new ArrayList<>();
list.add(aa);
list.add(bb);
list.add(cc);
//맵
Map<String, Object> map = new HashMap<>();
map.put("cc", cc);
model.addAttribute("aa",aa);
model.addAttribute("list",list);
model.addAttribute("map",map);
return "basic/variable";
}
variable.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>SpringEl 표현식</h1>
<h3>Object</h3>
<ul>
<li>${aa.name} = <span th:text="${aa.name}"></span>
<li>${aa['name']} = <span th:text="${aa['name']}"></span>
<li>${aa.getName()} = <span th:text="${aa.getName()}"></span>
</ul>
<h3>List</h3>
<ul>
<li>${list[1].name} = <span th:text="${list[1].name}"></span>
<li>${list[1]['name']} = <span th:text="${list[1]['name']}"></span>
<li>${list[1].getName()} = <span th:text="${list[1].getName()}"></span>
</ul>
<h3>Map</h3>
<ul>
<li>${map['cc'].name} = <span th:text="${map['cc'].name}"></span></li>
<li>${map['cc']['name']} = <span th:text="${map['cc']['name']}"></span></li>
<li>${map['cc'].getName()} = <span th:text="${map['cc'].getName()}"></span></li>
</ul>
<h3>지역변수 => (th:with)</h3>
<ul th:with="first = ${list[0]}">
<li>이름 = <span th:text="${first.name}"></span></li>
<li>나이 = <span th:text="${first.age}"></span></li>
</ul>
<ul th:with="second = ${list[1]}">
<li>이름 = <span th:text="${second.name}"></span></li>
<li>나이 = <span th:text="${second.age}"></span></li>
</ul>
</body>
</html>
'Spring Boot' 카테고리의 다른 글
DAY 89 - JPA (2024.11.12) (1) | 2024.11.12 |
---|---|
DAY 88 - JPA (2024.11.11) (0) | 2024.11.12 |
DAY 86 - Spring Boot DB연결 + Thymeleaf (2024.11.07) (1) | 2024.11.08 |
DAY 85 - Thymeleaf (2024.11.06) (0) | 2024.11.06 |
DAY 83 - Spring Boot / HOMEWORK (2024.11.04) (1) | 2024.11.04 |