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/map

true
<!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/map

Member2 [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/set

true
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/set

true

false
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-04

2019-03-04 17:01:54.702

www$baidu$cn

WWW.BAIDU.CN

www.baidu.cn

true

false

5

true

1

boot-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/set

true

false

5

true
如果真进行了集合类型的修改实际上发现所进行的页面操作也同样保持不变. 而且可以发现在页面之中都可以

根据索引取得的, 而不关心你是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/set

1

boot-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/set

www$baidu$cn

WWW.BAIDU.CN

www.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强大许多的.

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值