SpringBoot——Thymeleaf常见属性-字面量、字符串拼接、运算符

1 字面量、字符串拼接、运算符

字面量主要包含: 文本字面量( 用单引号'...'包围的字符串为文本字面量)、数字字面量、boolean 字面量、null 字面量

字符串拼接:用于拼接字符串

运算符: 三元运算:表达式?”正确结果”:”错误结果”
                算术运算:+ , - , * , / , %
                关系比较::> , < , >= , <= ( gt , lt , ge , le )
                相等判断:== , != ( eq , ne )

2 代码演示

User类

package com.liuhaiyang.springboot.entity;

//@Data 添加这个注解将不需要在写构造方法set、get等
public class User {
    private  Integer id;
    private  String name;
    private String phone;
    private String address;

//set()和get()
}

Controller类

package com.liuhaiyang.springboot.controller;

import com.liuhaiyang.springboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController2 {

    //字面量
    @RequestMapping("/listera1")
    public String listera1(Model model){
        model.addAttribute("sex",1);
        model.addAttribute("data","SpringBoot Data");
        model.addAttribute("flag",true);
        User user=new User();
        user.setId(100846);
        user.setName("李四");
        user.setPhone("123456");
        user.setAddress("张家界");
        model.addAttribute("user",user);
        User user1=new User();
        model.addAttribute("userDetail",user1);
        return "listera1";
    }
    //字符串拼接
    @RequestMapping("/page")
    public String splice(Model model){
        model.addAttribute("totalRows",123);
        model.addAttribute("totalPage",13);
        model.addAttribute("currentPage",2);
        return "splice";
    }

    //运算符
    @RequestMapping("/operator")
    public String operator(Model model){
        model.addAttribute("sex",1);
        model.addAttribute("flag",true);
        return "operator";
    }
}

核心配置文件

spring.thymeleaf.cache=false

2.1 字面量对应的html页面以及结果截图

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>字面量</title>
</head>
<body>
    <h1>文本字面量,用单引号‘。。。。’的字符串就是字面量</h1>
    <span th:text="hello"></span>

    <h1>数字字面量</h1>
今年是<span th:text="2021">1949</span>年 <br>
20年后是<span th:text="2021+20">1949</span>年 <br>

    <h1>boolean字面量</h1>
<div th:if="${flag}">执行成功</div>
<div th:if="${!flag}">执行失败</div>

<h1>null字面量</h1>
<span th:text="${user.name}"></span><br>
<div th:unless="${userDetail eq null}">对象已创建,地址不为空</div>
</body>
</html>

 

2.2 字符串拼接对应的html页面以及结果截图

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>字符串拼接</title>
</head>
<body>
 <span th:text="'共'+${totalRows}+'条'+${totalPage}+'页,当前第'+${currentPage}+'页,首页 上一页 下一页 尾页'">共120条12页,当前第1页,首页 上一页 下一页 尾页</span>

<h1>使用更优雅的方式来拼接字符串:|要拼接的字符串内容|</h1>

 <span th:text="|共${totalRows}条${totalPage}页,当前第${currentPage}页,首页 上一页 下一页 尾页|">共120条12页,当前第1页,首页 上一页 下一页 尾页</span>
</body>
</html>

2.3 运算符对应的html页面以及结果截图

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>运算符</title>
    </head>
    <body>
        <h1>三元运算符 表达式?正确:错误</h1>
        <div th:text="${sex eq 1 ? '男':'女'}"></div>
        <h1>算数运算</h1>
        20+5=<span th:text="20+5"></span><br/>
        20-5=<span th:text="20-5"></span><br/>
        20*5=<span th:text="20*5"></span><br/>
        20/5=<span th:text="20/5"></span><br/>
        21%5=<span th:text="21%5"></span><br/>
        <h1>关系比较</h1>
        5>2为<span th:if="5 gt 2">真</span><br>
        5<2为<span th:unless="5 lt 2">假</span> <br>
        1>=1为<span th:if="1 ge 1">真</span><br>
        1<=1为<span th:if="1 le 1">真</span><br>
        <h1>相等判断</h1>
        <span th:if="${sex==1}">男</span>
        <span th:if="${sex eq 1}">男</span>
        <span th:unless="${sex ne 1}">女</span>
    </body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值