基本语法实战案例01
- 在ThymeleafController中添加此方法
@RequestMapping("/show5")public String showInfo5(Model model) {model.addAttribute("msg", "Thymeleaf 第一个案例");model.addAttribute("key", new Date());return "index5";}
- 在src/main/resources/templates目录下面,新建index5.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Thymeleaf入门</title>
</head>
<body><span th:text="Hello"></span>
<hr/>
<span th:text="${msg}"></span>
<hr/>
<input type="text" name="username" th:value="${msg}"/>
<hr/>
<span th:text="${#strings.isEmpty(msg)}"/>
<hr/>
<span th:text="${#strings.contains(msg,'9')}"/>
<span th:text="${#strings.contains(msg,'T')}"/>
<hr/>
<span th:text="${#strings.startsWith(msg,'a')}"/>
<span th:text="${#strings.startsWith(msg,'T')}"/>
<hr/>
<span th:text="${#strings.endsWith(msg,'a')}"/>
<hr/>
<span th:text="${#strings.indexOf(msg,'h')}"/>
<hr/>
<span th:text="${#strings.substring(msg,13)}"/>
<span th:text="${#strings.substring(msg,13,14)}"/>
<hr/>
<span th:text="${#strings.toUpperCase(msg)}"/>
<span th:text="${#strings.toLowerCase(msg)}"/>
<hr/>
<span th:text="${#dates.format(key)}"/>
<hr/>
<span th:text="${#dates.format(key,'yyy/MM/dd')}"/>
<hr/>
<span th:text="${#dates.year(key)}"/>
<hr/>
<span th:text="${#dates.month(key)}"/>
<hr/>
<span th:text="${#dates.day(key)}"/>
</body>
</html>
- 启动项目,访问浏览器:http://localhost:8080/show5
基本语法实战案例02
- 在ThymeleafController中添加此方法
@RequestMapping("/show2")public String showInfo2(Model model) {model.addAttribute("sex", "男");model.addAttribute("id", "2");return "index2";}
- 在src/main/resources/templates目录下面,新建index2.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Thymeleaf入门</title>
</head>
<body><span th:if="${sex}=='男'">性别:男</span><span th:if="${sex}=='女'">性别:女</span><div th:switch="${id}"><span th:case="1">id为1</span><span th:case="2">id为2</span><span th:case="3">id为3</span></div>
</body>
</html>
- 启动项目,访问浏览器:http://localhost:8080/show2
基本语法实战案例03
- 在ThymeleafController中添加此方法
@RequestMapping("/show3")public String showInfo3(Model model) {List<User> list = new ArrayList<>();list.add(new User(1, "zhangsan", 20));list.add(new User(2, "lisi", 21));list.add(new User(3, "wangwu", 22));model.addAttribute("list", list);return "index3";}
- 在src/main/resources/templates目录下面,新建index3.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Thymeleaf入门</title>
</head>
<body>
<table border="1"><tr><th>ID</th><th>Name</th><th>Age</th><th>Index</th><th>Count</th><th>Size</th><th>Even</th><th>Odd</th><th>First</th><th>Last</th><th>Current</th></tr><tr th:each="u,var:${list}"><td th:text="${u.userid}"></td><td th:text="${u.username}"></td><td th:text="${u.userage}"></td><td th:text="${var.index}"></td><td th:text="${var.count}"></td><td th:text="${var.size}"></td><td th:text="${var.even}"></td><td th:text="${var.odd}"></td><td th:text="${var.first}"></td><td th:text="${var.last}"></td><td th:text="${var.current}"></td></tr>
</table>
</table>
</body>
</html>
- 启动项目,访问浏览器:http://localhost:8080/show3
基本语法实战案例04
- 在ThymeleafController中添加此方法
@RequestMapping("/show4")public String showInfo4(Model model) {Map<String, Object> map = new HashMap<>();map.put("u1", new User(1, "zhangsan", 20));map.put("u2", new User(2, "lisi", 21));map.put("u3", new User(3, "wangwu", 22));model.addAttribute("map", map);return "index4";}
- 在src/main/resources/templates目录下面,新建index4.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Thymeleaf入门</title>
</head>
<body>
<table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:text="${maps}"></td></tr>
</table>
<table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:each="entry:${maps}" th:text="${entry.key}"></td><td th:each="entry:${maps}" th:text="${entry.value}"></td></tr>
</table>
<table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:each="entry:${maps}" th:text="${entry.key}"></td><td th:each="entry:${maps}" th:text="${entry.value.username}"></td></tr>
</table>
<table border="1"><tr><th>ID</th><th>Name</th><th>Age</th></tr><tr th:each="maps :${map}"><td th:each="entry:${maps}" th:text="${entry.value.userid}"></td><td th:each="entry:${maps}" th:text="${entry.value.username}"></td><td th:each="entry:${maps}" th:text="${entry.value.userage}"></td></tr>
</table>
</body>
</html>
- 启动项目,访问浏览器:http://localhost:8080/show4
本文源码下载:
github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-7