在 thymeleaf 之中提供有相应的集合的处理方法,例如:在使用 List 集合的时候可以考虑采用 get()方法获取指定索引的数据,那么在使用 Set 集合的时候会考虑使用 contains()来判断某个数据是否存在,使用Map 集合的时候也希望可以使用 containsKey()判断某个 key 是否存在,以及使用get()根据 key 获取对应的 value,而这些功能在之前并不具备,下面来观察如何在页面中使用此类操作1、通过Map集合获取信息:member_map.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#maps.containsKey(allUsers,'baidu-6')}"/>
</body>
</html>http://localhost/member/maptrue
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#maps.containsKey(allUsers,'baidu-6')}"/><p th:text="${allUsers['baidu-6']}"/>
</body>
</html>http://localhost/member/mapMember2 [mid=107, name=赵四 - 6, age=9, birthday=Mon Mar 04 16:51:10 CST
2019, salary=99999.99]
2、判断某个数据是否存在:建立一个控制器的方法,该方法返回一个Set集合的内容@RequestMapping(value = "/member/set", method = RequestMethod.GET)public String set(Model model) {Set<String> allNames = new HashSet<String>() ;List<Integer> allIds = new ArrayList<Integer>() ;for (int x = 0 ; x < 5 ; x ++) {allNames.add("boot-" + x) ;allIds.add(x) ;}model.addAttribute("names", allNames) ;model.addAttribute("ids", allIds) ;model.addAttribute("mydate", new java.util.Date()) ;return "member/member_set" ;}
数据处理member_set.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#sets.contains(names,'boot-0')}"/>
</body>
</html>http://localhost/member/settrue
member_set.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#sets.contains(names,'boot-0')}"/><p th:text="${#sets.contains(names,'boot-9')}"/>
</body>
</html>http://localhost/member/settruefalse
member_set.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#dates.format(mydate,'yyyy-MM-dd')}"/><p th:text="${#dates.format(mydate,'yyyy-MM-dd HH:mm:ss.SSS')}"/><hr/><p th:text="${#strings.replace('www.baidu.cn','.','$')}"/><p th:text="${#strings.toUpperCase('www.baidu.cn')}"/><p th:text="${#strings.trim('www.baidu.cn')}"/><hr/><p th:text="${#sets.contains(names,'boot-0')}"/><p th:text="${#sets.contains(names,'boot-9')}"/><p th:text="${#sets.size(names)}"/><hr/><p th:text="${#sets.contains(ids,0)}"/><p th:text="${ids[1]}"/><p th:text="${names[1]}"/>
</body>
</html>2019-03-042019-03-04 17:01:54.702www$baidu$cnWWW.BAIDU.CNwww.baidu.cntruefalse5true1boot-3
3、如果说这个时候所返回的集合不是Set集合,而是List集合,只需要将"#sets"更换为"#lists"即可.建立一个控制器:@RequestMapping(value = "/member/set", method = RequestMethod.GET)public String set(Model model) {List<String> allNames = new ArrayList<String>() ;List<Integer> allIds = new ArrayList<Integer>() ;for (int x = 0 ; x < 5 ; x ++) {allNames.add("boot-" + x) ;allIds.add(x) ;}model.addAttribute("names", allNames) ;model.addAttribute("ids", allIds) ;model.addAttribute("mydate", new java.util.Date()) ;return "member/member_set" ;}member_set.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#sets.contains(names,'boot-0')}"/><p th:text="${#sets.contains(names,'boot-9')}"/><p th:text="${#sets.size(names)}"/><hr/><p th:text="${#sets.contains(ids,0)}"/>
</body>
</html>http://localhost/member/settruefalse5true
如果真进行了集合类型的修改实际上发现所进行的页面操作也同样保持不变. 而且可以发现在页面之中都可以根据索引取得的, 而不关心你是Set集合还是List集合:<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${ids[1]}"/><p th:text="${names[1]}"/>
</body>
</html>http://localhost/member/set1boot-1
4、在进行数据处理的时候字符串数据也是一种常见类型,所以在thymeleaf之中也支持有字符串的处理操作<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#strings.replace('www.baidu.cn','.','$')}"/><p th:text="${#strings.toUpperCase('www.baidu.cn')}"/><p th:text="${#strings.trim('www.baidu.cn')}"/>
</body>
</html>http://localhost/member/setwww$baidu$cnWWW.BAIDU.CNwww.baidu.cn
5、日期格式化:@RequestMapping(value = "/member/set", method = RequestMethod.GET)public String set(Model model) {List<String> allNames = new ArrayList<String>() ;List<Integer> allIds = new ArrayList<Integer>() ;for (int x = 0 ; x < 5 ; x ++) {allNames.add("boot-" + x) ;allIds.add(x) ;}model.addAttribute("names", allNames) ;model.addAttribute("ids", allIds) ;model.addAttribute("mydate", new java.util.Date()) ;return "member/member_set" ;}member_set.html<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head><title>SpringBoot模板渲染</title><link rel="icon" type="image/x-icon" href="/images/favicon.ico" /><meta http-equiv="Content-Type" content="text/html;charse=UTF-8">
</head>
<body><p th:text="${#dates.format(mydate,'yyyy-MM-dd')}"/><p th:text="${#dates.format(mydate,'yyyy-MM-dd HH:mm:ss.SSS')}"/>
</body>
</html>
可以发现模板的页面设计真的要比传统的JSP强大许多的.

SpringBoot 数据处理相关推荐

  1. SpringBoot+Vue+OpenLayers6完成前后端分离的“疫情地图”实战项目(一、地图数据处理及代码托管)

    前面我们介绍了Vue+webpack+openlayer的地图基础知识,从这一章开始,我们将正式开始我们的基于SpringBoot+Vue+OpenLayers的实战项目---疫情地图! 一.疫情地图 ...

  2. SpringBoot实战(十四)之整合KafKa

    本人今天上午参考了不少博文,发现不少博文不是特别好,不是因为依赖冲突问题就是因为版本问题. 于是我结合相关的博文和案例,自己改写了下并参考了下,于是就有了这篇文章.希望能够给大家帮助,少走一些弯路. ...

  3. angular图片传到后台_告诉你,SpringBoot+Angular有多牛逼!

    点击上方[全栈开发者社区]→右上角[...]→[设为星标⭐] 零 本文基于<SpringBoot+Angular入门实例教程>第5.1节的内容理解并简化而来.本文的目的浅析前后台分离的普通 ...

  4. SpringBoot接口频繁超时,长时间找不到原因,我用 Arthas 定位到了

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 公司有个渠道系统,专门对接三方渠道使用,没有什么业务逻辑, ...

  5. SpringBoot之错误处理机制

    文章目录 1.SpringBoot默认的错误处理机制 2.错误处理原理 (1)DefaultErrorAttributes (2)BasicErrorController:处理默认的/error请求 ...

  6. springboot整合elasticJob实战(纯代码开发三种任务类型用法)以及分片系统,事件追踪详解...

    一 springboot整合 介绍就不多说了,只有这个框架是当当网开源的,支持分布式调度,分布式系统中非常合适(两个服务同时跑不会重复,并且可灵活配置分开分批处理数据,贼方便)! 这里主要还是用到zo ...

  7. 完美实现SpringBoot+Angular普通登录

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:2020,搞个 Mac 玩玩!个人原创+1博客:点击前往,查看更多 作者:LYX6666 链接:https:// ...

  8. 使用 Arthas 排查 SpringBoot 诡异耗时的 Bug

    作者 | 空无 来源 | 阿里巴巴云原生公众号 背景 公司有个渠道系统,专门对接三方渠道使用,没有什么业务逻辑,主要是转换报文和参数校验之类的工作,起着一个承上启下的作用. 最近在优化接口的响应时间, ...

  9. SpringBoot整合SpringBatch实用简例

    SpringBatch主要是一个轻量级的大数据量的并行处理(批处理)的框架. 作用和Hadoop很相似,不过Hadoop是基于重量级的分布式环境(处理巨量数据),而SpringBatch是基于轻量的应 ...

最新文章

  1. 小程序登录、用户信息相关接口调整说明
  2. 0x63.图论 - 树的直径与最近公共祖先
  3. Sorting It All Out--POJ 1094
  4. java timezone 107_java - Java使用TimeZone - 堆栈内存溢出
  5. 如何在vue-cli3中使用tinymce
  6. docker-compose 实战案例
  7. qdialog 返回值_c – QDialog exec()并获取结果值
  8. 计算机一级误差怎么计算,(excel最大偏差公式)偏离值怎么计算
  9. Ardunio开发实例-ADS1115模数转换器
  10. Unity 2D游戏制作流程用到的技巧
  11. 追忆似水流年,似水高三
  12. 去掉台电U盘加密大师
  13. 一看就懂的网络协议五层模型(一)
  14. Consul Consensus Protocol
  15. 项目中发生的一个奇葩问题
  16. 计算机网络_03_传输层(个人总结)
  17. VMware:速修复这三个严重的 Workspace ONE Assist 软件漏洞
  18. 工行融e联,绿色通道便捷办理
  19. 被遗忘的黑莓,你还活得好么?
  20. 2021年广东专插本计算机专业学校,2021年广东省专插本学校名单和专业,广东专插本有哪些学校和那些专业...

热门文章

  1. 如何修改被编译后DLL文件
  2. Android联系人Contacts详解
  3. python sqlalchemy mysql 自动映射
  4. flex页面布局练习--知乎
  5. 洛谷P1155 双栈排序
  6. Android測试APP工具(一)
  7. 洛谷P1561 [USACO12JAN]爬山Mountain Climbing 贪心 数学
  8. UIDocumentInteractionController 文件预览
  9. matlab使用常犯的错误
  10. Ubuntu 中启用 root 帐号