728x90
반응형
Thymeleaf
Integrations galore Eclipse, IntelliJ IDEA, Spring, Play, even the up-and-coming Model-View-Controller API for Java EE 8. Write Thymeleaf in your favourite tools, using your favourite web-development framework. Check out our Ecosystem to see more integrati
www.thymeleaf.org





package com.bit.controller;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.bit.domain.MemberVo;
@Controller
public class SampleController {
@GetMapping("/sample1") //templates/sample1.html (동적) thymeleaf 가 들어가는 자리
public void sample1(Model model) {
model.addAttribute("greeting", "반갑습니다");
}
@GetMapping("/sample2")
public void sample2(Model model) {
MemberVo vo = new MemberVo(123, "u00", "p00", "홍길동", new Timestamp(System.currentTimeMillis()));
model.addAttribute("vo",vo);
}
@GetMapping("/sample3")
public void sample3(Model model) {
List<MemberVo> list = new ArrayList<MemberVo>();
for (int i = 0; i < 10; i++) {
list.add(new MemberVo(123, "u0" + i, "p0" + i, "홍길동" + i, new Timestamp(System.currentTimeMillis())));
}
model.addAttribute("list", list);
}
// 삼항연산자와 조건문
@GetMapping("/sample4")
public void sample4(Model model) {
List<MemberVo> list = new ArrayList<MemberVo>();
for (int i = 0; i < 10; i++) {
list.add(new MemberVo(123, "u000" + i%3, "p000" + i%3, "홍길동" + i, new Timestamp(System.currentTimeMillis())));
}
model.addAttribute("list", list);
}
@GetMapping("/sample5")
public void sample5(Model model) {
model.addAttribute("result", "SUCCESS");
}
}


<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Thymeleaf Test Page</h1>
<h1 th:text="${greeting}">Thymeleaf Test Page</h1>
</body>
</html>

<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf2</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3 th:text = "${vo}">Thymeleaf Test Page</h3>
<div th:utext = "${'<h3>' + vo.mid + '</h3>'}"></div>
<div th:text = "${'<h3>' + vo.mid + '</h3>'}"></div>
</body>
</html>

<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf3</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<table border="1">
<tr>
<td>INDEX</td>
<td>SIZE</td>
<td>ODD/EVEN</td>
<td>MID</td>
<td>MNAME</td>
<td>REGDATE</td>
</tr>
<!--iterState : each의 상태변수-->
<tr th:each = "member, iterState : ${list}">
<td th:text = "${iterState.index}"></td>
<td th:text = "${iterState.size}"></td>
<td th:text = "${iterState.odd} + '/' + ${iterState.even}"></td> <!--짝수나 홀수냐 여붓값-->
<td th:text = "${member.mid}"></td>
<td th:text = "${member.mname}"></td>
<!--dates는 날짜 보조객체-->
<td th:text = "${#dates.format(member.regdate,'yyyy-mm-dd')}"></td>
</tr>
</table>
</body>
</html>


<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf4</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<!--th:with taget : 지정한 이 영영(여기서는 테이블) 에서만 사용되는 지역변수-->
<table border="1" th:with = "target = 'u0001'">
<tr>
<td>MID</td>
<td>MNAME</td>
<td>REGDATE</td>
</tr>
<!--iterState : each의 상태변수-->
<tr th:each = "member : ${list}">
<td th:text = "${member.mid==target? 'SECRET':member.mid}"></td>
<td th:text = "${member.mname}"></td>
<!--dates는 날짜 보조객체-->
<td th:text = "${#dates.format(member.regdate,'yyyy-mm-dd')}"></td>
</tr>
</table><p>
<table border="1" th:with = "target = 'u0001'">
<tr>
<td>MID</td>
<td>MNAME</td>
<td>REGDATE</td>
</tr>
<!--iterState : each의 상태변수-->
<tr th:each = "member : ${list}">
<td>
<a href="/modify" th:if = "${member.mid==target}" th:text = "MODIFY"></a>
<p th:unless = "${member.mid==target}" th:text = "VIEW"></p>
</td>
<td th:text = "${member.mid==target? 'SECRET':member.mid}"></td>
<td th:text = "${member.mname}"></td>
<td th:text = "${#dates.format(member.regdate,'yyyy-mm-dd')}"></td>
</tr>
</table><p>
</body>
</html>

<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf4</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<script>
var result = [[${result}]];
</script>
<script th:inline = "javascript">
var result = [[${result}]];
</script>
</body>
</html>728x90
반응형
'Spring > Spring framework' 카테고리의 다른 글
| 인터셉터 (0) | 2021.08.19 |
|---|---|
| boot06 부트스트랩 이용한 게시판 만들기 (0) | 2021.08.11 |
| boot05 (0) | 2021.08.11 |
| JPA (0) | 2021.08.09 |
| boot04 회원게시판 (0) | 2021.08.09 |
| [JPA] boot3 데이터 DB 입출력 (0) | 2021.08.09 |