当前位置: 首页 > 图灵资讯 > 技术篇> Thymeleaf的属性2

Thymeleaf的属性2

来源:图灵教育
时间:2023-06-13 09:26:38

1.字面量1121.1文字面量112

用单引号'...“包围的字符串是文本字面量

<p th:text=“城市”+${city}+' 用户登录'+${isLogin}"></p>

1.2数字面量112

<p th:if="${age > 10}"> age > 10 </p><p th:if="20 > 5">20 大于 5</p>

1.3boolean字面量112

<p th:if="${isLogin == true}">用户登录了</p>

1.1和1.2和1.3代码如下

// 字面量   112    @GetMapping("/text")    public String text(Model model){        model.addAttribute("sex","m");        model.addAttribute("age",20);        model.addAttribute("name"张三";        model.addAttribute("city","北京";        model.addAttribute("isLogin",true);        model.addAttribute("myuser" ,new SysUser(1005,“周峰”,“男”,20);        return "text";    }

test.html

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head>    <meta charset="UTF-8">    <title>字面量  112</title></head><body>    <p style="margin-left: 400px">       <h3>文字面量: 使用单引号包含的字符串</h3>       <p th:text="'我是'+${name}+',我所在的城市+${city}">数据显示</p>       <h3>数字字面量</h3>        <p th:if="${20>5}"> 20大于 5</p>        <h3>boolean字面量</h3>        <p th:if="${isLogin == true}">用户已登录系统</p>        <h3>null字面量</h3>        <p th:if="${myuser != null}">有myuser数据</p>    </p></body></html>

Thymeleaf的属性2_Thymeleaf

1.4字符串连接113

连接字符串有两种语法

1)语法用单引号包括字符串,用+连接其他字符串或表达式

 <p th:text="'我是'+${name}+我所在的城市,我所在的城市+${city}">数据显示</p>

2)语法:使用双竖线|字符串和表达式|

<p th:text="|我是${name},我所在的城市${city|>    显示数据

///字符串连接   113    @GetMapping("/strjoin")    public String strJoin(Model model){        model.addAttribute("sex","m");        model.addAttribute("age",20);        model.addAttribute("name""李思";        model.addAttribute("city","上海";        model.addAttribute("isLogin",true);        model.addAttribute("myuser" ,new SysUser(1005,“周峰”,“男”,20);        return "strjoin";    }

strjoin.html

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head>    <meta charset="UTF-8">    <title>字符串连接   113</title></head><body>    <p style="margin-left: 400px">       <h3>字符串连接方式1:使用单引号包含的字符串</h3>       <p th:text="'我是'+${name}+',我所在的城市是‘’+${city}">数据显示</p>        <br/>        <br/>        <h3>字符串连接方式2:|字符串和表达式|</h3>        <p th:text="|我是${name},所在城市${city},其他人${myuser.name}|"></p>    </p></body></html>

Thymeleaf的属性2_Thymeleaf_02

1.5运算符114

算术操作:+,*,/,%,

关系比较:>,<,>=,<=(gt,lt,ge,le)

相等判断:==,!=(eq,ne)

三元运算符:

表达式?true结果:false结果

可嵌套三元运算符

///使用操作符   114    @GetMapping("/sym")    public String sym(Model model){        model.addAttribute("sex","m");        model.addAttribute("age",20);        model.addAttribute("name""李思";        model.addAttribute("city","上海";        model.addAttribute("isLogin",true);        model.addAttribute("myuser" , null);        return "sym";    }

sym.html

<!DOCTYPE html><html lang="en" xmlns:th="http://www.thymeleaf.org"><head>    <meta charset="UTF-8">    <title>运算符   114</title></head><body>    <p style="margin-left: 400px">        <h3>使用运算符</h3>        <p th:text="${age > 10}">年龄大于 10 </p>        <p th:text="${ 20 + 30 }">显示运算结果</p>        <p th:if="${myuser == null}">myuser是null</p>        <p th:if="${myuser eq null}">myuser是null</p>        <p th:if="${myuser ne null}">myuser不是nuller</p>        <p th:text="${isLogin == true ? 用户已经登录了 : “用户需要登录”></p>        <p th:text="${isLogin == true ? 用户已经登录了 : “用户需要登录”></p>        <p th:text="${isLogin == true ? ( age > 10 ? “用户大于10” : “用户年龄较小” : “用户需要登录”></p>    </p></body></html>

Thymeleaf的属性2_字面量_03