SpringBoot 从零开始学习(八) 之视图层技术thymeleaf基本操作使用
2020-03-08 12:24
489 查看
Thymeleaf 处理字符串/处理日期/ 条件判断/循环迭代
pom.xml中增加Thymeleaf的坐标:
<!-- themeleaf 启动器坐标 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
controller模拟发送数据进行处理:
package com.cz.controller; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.cz.pojo.User; import com.sun.org.apache.bcel.internal.generic.NEW; @Controller public class ThymeleafController { @RequestMapping("/showUser") public String showUser(Model model) { /*start 这里是传递list*/ List userList = new ArrayList<User>(); userList.add(new User(1, "张三", 20)); userList.add(new User(2,"李四",30)); userList.add(new User(44,"王五",40)); model.addAttribute("users", userList); /*end 这里是传递list*/ /*start 这里是传递text*/ model.addAttribute("msg", "我们的祖国是花园abc"); /*end 这里是传递text*/ /*start 这里是传递date*/ model.addAttribute("date", new Date()); /*end 这里是传递date*/ /*start if判断*/ model.addAttribute("sex", "男"); /*end if判断*/ /*start switch判断*/ model.addAttribute("id", 3); /*end switch判断*/ /*start map循环*/ Map<String, User> map = new HashMap<String, User>(); map.put("user1",new User(1, "张三", 20)); map.put("user2",new User(2,"李四",30)); map.put("user3",new User(44,"王五",40)); model.addAttribute("maps", map); /*end map循环*/ return "showUser"; } }
Thymeleaf 视图展示处理:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Thymeleaf基本操作和使用</title> </head> <body> <span th:text="${msg}"></span> <hr> <input type="text" name="userName" th:value="输入input组建中写入数据"></input> <hr> <!-- 基于Thymeleaf的字符串操作 --> <span th:text="${#strings.isEmpty(msg)}"></span> <hr> <span th:text="${#strings.contains(msg,'我')}"></span> <br> <span th:text="${#strings.contains(msg,'他')}"></span> <hr> <span th:text="${#strings.startsWith(msg,'他')}"></span> <br> <span th:text="${#strings.startsWith(msg,'我')}"></span> <hr> <span th:text="${#strings.length(msg)}"></span> <hr> <span th:text="${#strings.indexOf(msg,'国')}"></span> <hr> <span th:text="${#strings.substring(msg,0,4)}"></span> <hr> <span th:text="${#strings.endsWith(msg,'园')}"></span> <hr> <span th:text="${#strings.toUpperCase(msg)}"></span> <hr> <span th:text="${#strings.toLowerCase(msg)}"></span> <hr> <!-- Thymeleaf对日期的操作 --> <span th:text="${date}"></span> <hr> <span th:text="${#dates.format(date)}"></span> <hr> <span th:text="${#dates.format(date,'yyyy-MM-dd')}"></span> <hr> <span th:text="${#dates.year(date)}"></span> <hr> <span th:text="${#dates.month(date)}"></span> <hr> <span th:text="${#dates.day(date)}"></span> <hr> <span th:text="${#dates.hour(date)}"></span> <hr> <!-- Thymeleaf 的条件判断 --> <span th:if="${sex} == '男'">性别是男</span> <span th:if="${sex} == '女'">性别是女</span> <hr> <div th:switch="${id}"> <span th:case="0">该学生的学号是0</span> <span th:case="1">该学生的学号是1</span> <span th:case="2">该学生的学号是2</span> <span th:case="3">该学生的学号是3</span> </div> <hr> <!-- Thymeleaf 的循环迭代 --> <table border="5" align="center" width="50%" > <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> <tr th:each="user:${users}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> </tr> </table> <hr> <!-- Thymeleaf each下面增加状态变量的的循环迭代 --> <table border="5" align="center" width="50%" > <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><!--是否最后一个--> </tr> <tr th:each="user,status:${users}"> <td th:text="${user.id}"></td> <td th:text="${user.name}"></td> <td th:text="${user.age}"></td> <td th:text="${status.index}"></td> <td th:text="${status.count}"></td> <td th:text="${status.size}"></td> <td th:text="${status.even}"></td> <td th:text="${status.odd}"></td> <td th:text="${status.first}"></td> <td th:text="${status.last}"></td> </tr> </table> <hr> <!-- Thymeleaf 的循环迭代map --> <table border="5" align="center" width="50%" > <tr> <th>Key</th> <th>ID</th> <th>Name</th> <th>Age</th> </tr> <tr th:each="user:${maps}"> <td th:each="entrykey:${user}" th:text="${entrykey.key}"></td> <td th:each="entrykey:${user}" th:text="${entrykey.value.id}"></td> <td th:each="entrykey:${user}" th:text="${entrykey.value.name}"></td> <td th:each="entrykey:${user}" th:text="${entrykey.value.age}"></td> </tr> </table> </body> </html>
执行效果:
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- SpringBoot 从零开始学习(九) 之视图层技术thymeleaf域对象操作以及URL表达式
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 【个人学习】使用idea搭建SpringBoot,整合Mybatis、Thymeleaf,连接数据库,实现具有前端界面项目:主要功能登录,注册,个人信息查看、更改,不定时更新中...
- SpringBoot学习之路:03.Spring Boot使用Jpa操作数据库
- springboot学习记录之 thymeleaf使用模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- Spring Boot 学习11--使用模版Thymeleaf- freemarker
- Springboot学习之路——thymeleaf语法使用
- Spring Boot学习笔记2——基本使用之最佳实践[z]
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- SpringBoot学习记录二、基本使用
- 深入学习spring-boot系列(三)--使用thymeleaf模板
- spring-boot-web-ui及thymeleaf基本使用
- 深入学习spring-boot系列(三)--使用thymeleaf模板