SpringBoot——Thymeleaf中的条件判断(th:if、th:unless、th:switch、th:case)

简介: SpringBoot——Thymeleaf中的条件判断(th:if、th:unless、th:switch、th:case)

1.写在前面


th:if、th:unless、th:switch、th:case 这几个属性,其实和JSP里面的那些标签都是类似的,含义就可以理解为Java语言中的if、else、switch-case这些条件判断一样,所以这里就不再详细叙述了,下面就直接给出例子!!!


2.应用举例


首先写一个控制层, 其中有一个请求方法。


package com.songzihao.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
 *
 */
@Controller
public class UserController {
    @RequestMapping(value = "/condition")
    public String condition(Model model) {
        model.addAttribute("sex","男");
        model.addAttribute("flag",true);
        model.addAttribute("status",0);
        return "condition";
    }
}

然后是对应的html页面。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>条件判断</title>
</head>
<body>
    <h2>th:if 用法:如果满足条件显示(执行),否则相反</h2>
    <div th:if="${sex eq '男'}">
        man
    </div>
    <div th:if="${sex eq '女'}">
        woman
    </div>
    <hr/>
    <h2>th:unless 用法:与th:if相反</h2>
    <div th:unless="${status != 1}">
        状态为0,不太行
    </div>
    <div th:unless="${status ne 0}">
        状态为1,真不错
    </div>
    <hr/>
    <h2>th:switch/th:case 用法</h2>
    <div th:switch="${flag}">
        <span th:case="true">真</span>
        <span th:case="false">假</span>
        <span th:case="*">无此布尔值</span>
    </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);
    }
}


相关文章
|
4月前
|
前端开发 Java Spring
SpringBoot项目thymeleaf页面支持词条国际化切换
SpringBoot项目thymeleaf页面支持词条国际化切换
115 2
|
6月前
|
Java 网络架构
springboot配合thymeleaf,调用接口不跳转页面只显示文本
springboot配合thymeleaf,调用接口不跳转页面只显示文本
259 0
|
7月前
|
前端开发 Java Spring
springboot+thymeleaf+bootstrap 超级无敌简洁的页面展示 商城管理页面
这篇文章展示了一个使用Spring Boot、Thymeleaf和Bootstrap框架开发的简洁、响应式的商城管理页面,包括美食介绍、产品详情、购物车等功能,适合初学者学习和使用。
springboot+thymeleaf+bootstrap 超级无敌简洁的页面展示 商城管理页面
|
7月前
|
Java 数据库 Spring
springboot+thymeleaf中前台页面展示中、将不同的数字替换成不同的字符串。使用条件运算符
这篇文章介绍了如何在Spring Boot和Thymeleaf框架中使用条件运算符来根据数字字段的值动态替换显示不同的字符串,例如将订单状态的数字0和1替换为"未付款"和"已付款"等。
springboot+thymeleaf中前台页面展示中、将不同的数字替换成不同的字符串。使用条件运算符
|
9月前
|
前端开发 Java Spring
Spring Boot中使用Thymeleaf进行页面渲染
Spring Boot中使用Thymeleaf进行页面渲染
|
前端开发 Java
SpringBoot-6-模板Thymeleaf常用标签
SpringBoot-6-模板Thymeleaf常用标签
108 0
SpringBoot-6-模板Thymeleaf常用标签
|
10月前
|
SQL 前端开发 JavaScript
Spring Boot + Thymeleaf 使用PageHelper实现分页
Spring Boot + Thymeleaf 使用PageHelper实现分页
126 0
|
10月前
|
SQL 前端开发 Java
springboot + thymeleaf + layui 初尝试
springboot + thymeleaf + layui 初尝试
|
10月前
|
Java
SpringBoot thymeleaf自定义错误页面
SpringBoot thymeleaf自定义错误页面
87 0
|
10月前
|
XML 前端开发 Java
Spring Boot的Web开发之Thymeleaf模板引擎的解析及使用(Thymeleaf的基础语法以及常用属性)
Spring Boot的Web开发之Thymeleaf模板引擎的解析及使用(Thymeleaf的基础语法以及常用属性)
237 0