當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot 数据处理
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot 数据处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在 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 数据处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 包含处理
- 下一篇: SpringBoot 基础上传操作