在 thymeleaf 之中提供有相应的集合的处理方法,例如:在使用 List 集合的时候能够考虑采用 get()方法获取指定索引的数据,那么在使用 Set 集合的时候会考虑使用 contains()来判断某个数据是否存在,使用 Map 集合的时候也但愿可使用 containsKey()判断某个 key 是否存在,以及使用get()根据 key 获取对应的 value,而这些功能在以前并不具有,下面来观察如何在页面中使用此类操做.javascript
1.经过map集合获取信息html
<p th:text="${#maps.containsKey(allMembers,'mldn-7')}"/> <p th:text="${allMembers['mldn-7'].name}"/>
member_map.htmljava
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>SpringBoot模版渲染</title> <script type="text/javascript" th:src="@{/js/main.js}"></script> <link rel="icon" type="image/x-icon" href="/images/mldn.ico"/> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/> </head> <body> <p th:text="${#maps.containsKey(allMembers,'mldn-7')}"/> <p th:text="${allMembers['mldn-7'].name}"/> <hr/> <table> <tr><td>No.</td><td>KEY</td><td>MID</td><td>姓名</td><td>年龄</td><td>偶数</td><td>奇数</td></tr> <tr th:each="memberEntry,memberStat:${allMembers}"> <td th:text="${memberStat.index + 1}"/> <td th:text="${memberEntry.key}"/> <td th:text="${memberEntry.value.mid}"/> <td th:text="${memberEntry.value.name}"/> <td th:text="${memberEntry.value.age}"/> <td th:text="${memberStat.even}"/> <td th:text="${memberStat.odd}"/> </tr> </table> </body> </html>
2.判断某一个数据是否存在web
创建一个控制器的方法,该方法返回一个set集合的内容spring
package cn.mldn.microboot.controller; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import cn.mldn.microboot.util.controller.AbstractBaseController; import cn.mldn.microboot.vo.Member; @Controller public class MemberController extends AbstractBaseController { @RequestMapping(value = "/member/set", method = RequestMethod.GET) public String set(Model model) { Set<String> allNames = new HashSet<String>() ; Set<Integer> allIds = new HashSet<Integer>() ; for (int x = 0 ; x < 5 ; x ++) { allNames.add("mldn-" + x) ; allIds.add(x) ; } model.addAttribute("names", allNames) ; model.addAttribute("ids", allIds) ; return "member/member_set" ; } }
创建一些页面进行一些数据的处理操做:app
<p th:text="${#sets.contains(names,'mldn-0')}"/> <p th:text="${#sets.contains(names,'mldn-9')}"/> <p th:text="${#sets.size(names)}"/>
<p th:text="${#sets.contains(ids,0)}"/>
3.若是说这个时候所返回的集合不是set集合,而是list集合,只须要将"#sets"更换为"#lists"便可。jsp
创建一个控制器:ui
@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("mldn-" + x) ; allIds.add(x) ; } model.addAttribute("names", allNames) ; model.addAttribute("ids", allIds) ; return "member/member_set" ; }
若是真进行了修改集合类型的修改实际上发现所进行页面操做也一样保持不变。并且能够发如今页面之中均可以根据索引取得了,而不关心你是set集合仍是list集合设计
<p th:text="${ids[1]}"/> <p th:text="${names[1]}"/>
4.在进行数据处理的时候字符串数据也是一个常见类型,因此在thymeleaf之中也支持有字符串的处理操做。code
<p th:text="${#strings.replace('www.mldn.cn','.','$')}"/> <p th:text="${#strings.toUpperCase('www.mldn.cn')}"/> <p th:text="${#strings.trim('www.mldn.cn')}"/>
5.日期格式化
@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("mldn-" + x) ; allIds.add(x) ; } model.addAttribute("names", allNames) ; model.addAttribute("ids", allIds) ; model.addAttribute("mydate", new java.util.Date()) ; return "member/member_set" ; }
<p th:text="${#dates.format(mydate,'yyyy-MM-dd')}"/> <p th:text="${#dates.format(mydate,'yyyy-MM-dd HH:mm:ss.SSS')}"/>
能够发现模板的页面设计真的要比传统的jsp强大许多。