基本语法实战案例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

第七篇:Spring Boot整合Thymeleaf_入门试炼03相关推荐

  1. 第七篇:Spring Boot整合Thymeleaf_入门试炼02

    Thymeleaf 语法详解: 变量输出与字符串操作 变量输出与字符串操作 th:text 在页面中输出值 变量输出与字符串操作 th:value 可以将一个值放入到input标签的value中 判断 ...

  2. 第八篇:Spring Boot整合Thymeleaf_入门试炼04

    继承parent父工程,新建一个子项目,名称为spring-boot-chapter-8 1.引入 thymeleaf 模板依赖 <!-- 引入 thymeleaf 模板依赖 -->< ...

  3. 第七篇:Spring Boot 整合_Thymeleaf 入门试炼 QuickStart

    1.1 添加依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId> ...

  4. Elasticsearch实战篇——Spring Boot整合ElasticSearch

    2019独角兽企业重金招聘Python工程师标准>>> 当前Spring Boot很是流行,包括我自己,也是在用Spring Boot集成其他框架进行项目开发,所以这一节,我们一起来 ...

  5. ElasticSearch实战篇 - Spring Boot 整合 ElasticSearch

    点击上方 Java后端,选择 设为星标 优质文章,及时送达 作者:冯文议 链接:segmentfault.com/a/1190000018625101 当前Spring Boot很是流行,包括我自己, ...

  6. eureka集群只注册一个_Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  7. Spring Boot整合Redis以及Redis的原理

    Redis的原理及知识 Redis简介 redis是一个key-value.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合). ...

  8. spring boot整合SpringSecurity-03 自定义报错信息

    spring boot整合SpringSecurity 目录 spring boot整合SpringSecurity-01入门 spring boot整合SpringSecurity-02 基于Ser ...

  9. MongoDB简单入门篇及其Spring Boot整合

    参考视频 ① MongoDB简介 简介 MongoDB是为快速开发互联网Web应用而设计的数据库系统 MongoDB的设计目标是极简.灵活. 作为Web应用栈的一部分 MongoDB是No SQL数据 ...

最新文章

  1. 一元操作符和使用Number()方法的区别
  2. eclipse 配置java路径_Java修改eclipse中web项目的server部署路径问题
  3. 用python画玫瑰花教程-使用Python画一朵玫瑰花
  4. mysql 临时表增加主键_MySQL之重建表
  5. 【POJ - 2398】Toy Storage (计算几何,二分找位置,叉积,点和直线的位置关系)
  6. mysql window下 安装udf_最新版MySQL在window平台安装
  7. 批处理不同目录下的bat调用
  8. ASP.NET错误处理的方式(一)
  9. 华为交换机ensp基础命令
  10. myeclipse php插件phpeclipse安装及配置(插件式安装)
  11. linux系统刷苹果4s,iOS8.4降级6.1.3教程 iPhone4s降级iOS6.1.3
  12. 如何在 vuePress中添加博客导流公众号-即输入验证码解锁全站文章
  13. Android照片处理——涂鸦和拼图实现
  14. 解决swiper动态改变数据后分页混乱问题
  15. 【CV】FPN:用于目标检测的特征金字塔网络
  16. plc通讯块FC5、FC6
  17. 《牛客刷verilog》Part I Verilog快速入门
  18. kafka的基本概念和工作流程分析
  19. iphone6 触摸屏测试软件,iPhone 6 触摸屏 “乱跳” 维修案例
  20. 码科速送同城跑腿小程序V2.7.4+骑手端+前端

热门文章

  1. 23种设计模式之工厂方法模式
  2. java文件流操作注意
  3. node.js包管理器和代码调式
  4. Apache Kylin从入门到精通
  5. 【详谈 Delta Lake 】系列技术专题 之 特性(Features)
  6. 阿里云Kubernetes CSI实践—NAS动态存储卷使用
  7. AutoML数据增广
  8. 阿里小二的日常工作要被TA们“接管”了!
  9. 如何利用 Webshell 诊断 EDAS Serverless 应用
  10. 基于阿里云实现游戏数据运营(附Demo)