SpringBoot——Thymeleaf中的四种字面量(文本、数字、布尔、null)、字符串拼接、运算符

简介: SpringBoot——Thymeleaf中的四种字面量(文本、数字、布尔、null)、字符串拼接、运算符

1.四种字面量


首先写一个User类、以及控制层UserController类, 其中有一个请求方法。


package com.songzihao.springboot.model;
/**
 *
 */
public class User {
    private Integer id;
    private String username;
    //getter and setter
}


package com.songzihao.springboot.controller;
import com.songzihao.springboot.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 *
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {
    @RequestMapping(value = "/literal")
    public String literal(Model model) {
        model.addAttribute("sex",1);
        model.addAttribute("data","SpringBoot Thymeleaf");
        model.addAttribute("flag",true);
        User user=new User();
        user.setId(1001);
        user.setUsername("张起灵");
        model.addAttribute("user",user);
        User userDetail=new User();
        model.addAttribute("userDetail",userDetail);
        return "literal";
    }
}

然后是这个请求方法对应的html页面。(其中包含了四种字面量的声明与使用)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>字面量</title>
</head>
<body>
    <h3>文本字面量,用单引号 '......' 的字符串就是字面量</h3>
    <a th:href="@{'/user/literal?sex=' + ${sex}}">查看性别</a>
    <hr/>
    <h3>数字字面量</h3>
    今年是 <span th:text="2020">1945</span>年<br/>
    20年后是 <span th:text="2020+20">1965</span>年<br/>
    <hr/>
    <h3>boolean字面量</h3>
    <div th:if="${flag}">
        执行成功!!!
    </div>
    <div th:unless="${flag}">
        执行失败。。。
    </div>
    <hr/>
    <h3>null字面量</h3>
    <span th:if="${user ne null}">用户不为空</span>
    <div th:unless="${userDetail eq null}">
        对象已创建,不为空
    </div>
    <div th:if="${userDetail.id eq null}">
        userDetail的id为空
    </div>
</body>
</html>


最后在核心配置文件中关闭Thymeleaf的页面缓存开关,之后,启动入口类进行测试。


spring.thymeleaf.cache=false
package com.songzihao.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}



2.字符串拼接


使用 | 进行字符串拼接。



3.运算符


三元运算:表达式?” 正确结果”:” 错误结果”

算术运算: :+ , - , * , / , %

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

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

相关文章
|
21天前
|
Java Spring
解决Springboot集成ElasticSearch 报错:A bean with that name has already been defined in null and overriding
解决Springboot集成ElasticSearch 报错:A bean with that name has already been defined in null and overriding
|
1月前
|
Java 测试技术 Maven
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
|
1月前
|
SQL 前端开发 JavaScript
Spring Boot + Thymeleaf 使用PageHelper实现分页
Spring Boot + Thymeleaf 使用PageHelper实现分页
|
1月前
|
Java
SpringBoot中静态类使用配置文件经常遇到读取为NULL的情况,现在我就告诉大家。
SpringBoot中静态类使用配置文件经常遇到读取为NULL的情况,现在我就告诉大家。
25 0
|
1月前
|
JSON 前端开发 Java
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之Spring定义Jackson转换Null的方法和实现案例
【SpringBoot实战专题】「开发实战系列」全方位攻克你的技术盲区之Spring定义Jackson转换Null的方法和实现案例
63 0
|
1月前
|
SQL 前端开发 Java
springboot + thymeleaf + layui 初尝试
springboot + thymeleaf + layui 初尝试
|
1月前
|
Java 数据库连接 API
SpringBoot【问题 01】借助@PostConstruct解决使用@Component注解的类用@Resource注入Mapper接口为null的问题(原因解析+解决方法)
SpringBoot【问题 01】借助@PostConstruct解决使用@Component注解的类用@Resource注入Mapper接口为null的问题(原因解析+解决方法)
172 0
|
1月前
|
XML 前端开发 Java
Spring Boot的Web开发之Thymeleaf模板引擎的解析及使用(Thymeleaf的基础语法以及常用属性)
Spring Boot的Web开发之Thymeleaf模板引擎的解析及使用(Thymeleaf的基础语法以及常用属性)
81 0
|
1月前
|
移动开发 Java HTML5
Thymeleaf - 与SpringBoot整合入门
Thymeleaf - 与SpringBoot整合入门
48 0
|
8月前
|
Java 数据库 Spring
spring boot 查询到的数据返回null
spring boot 查询到的数据返回null
271 0

热门文章

最新文章