• Thymeleaf常用语法:条件判断 if、switch case


    if语句
    条件判断使用th:if,它会判断表达式是否成立,表达式的结果支持boolean、number、character、String及其他类型。
    满足下面情况,if语句成立:
    (1) 表达式的结果是数字且不是0
    (2) 表达式的结果是字符串且不是false、off、no、0
    (3) 表达式的结果是其他数据类型
    switch case语句
    (1) 类似Java的switch case语句:th:switch、th:case
    (2) 使用th:case="*"来表示默认值
    (3) 如果第一个th:case结果为true,则其它所有的th:case会被设置为false,即使它们的结果也是true

    开发环境:IntelliJ IDEA 2019.2.2
    Spring Boot版本:2.1.8

    新建一个名称为demo的Spring Boot项目。

    1、pom.xml
    加入Thymeleaf依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>

    2、src/main/java/com/example/demo/TestController.java

    package com.example.demo;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Controller
    public class TestController {
        @RequestMapping("/")
        public String test(Model model){
            List<Integer> list = new ArrayList<Integer>();
            list.add(1);
            model.addAttribute("list", list);
            model.addAttribute("flag", true);
            model.addAttribute("gender", 1);
            return "test";
        }
    }

    3、src/main/resources/templates/test.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h4>一、条件判断</h4>
         [1]<div th:text="true" th:if="${flag}"></div>
         [2]<div th:text="'数字,非0'" th:if="10"></div>
         [3]<div th:text="'字符串,非false、off、no'" th:if="'a'"></div>
         [4]<div th:text="其他数据类型" th:if="${list}"></div>
         [5]<div th:text="字符串0" th:if="'0'"></div>
         [6]<div th:text="数字0" th:if="0"></div>
         [7]<div th:text="false" th:if="'false'"></div>
         [8]<div th:text="off" th:if="'off'"></div>
         [9]<div th:text="no" th:if="'no'"></div>
    <h4>二、switch case</h4>
        <select th:switch="${gender}">
            <option th:case="1"></option>
            <option th:case="0"></option>
            <option th:case="*">其他</option>
        </select>
    
        <select th:switch="2">
            <option th:case="1"></option>
            <option th:case="0"></option>
            <option th:case="*">其他</option>
        </select>
    
    </body>
    </html>

    浏览器访问:http://localhost:8080
    右键查看网页源代码,生成的HTML源码:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h4>一、条件判断</h4>
         [1]<div>true</div>
         [2]<div>数字,非0</div>
         [3]<div>字符串,非false、off、no</div>
         [4]<div>其他数据类型</div>
         [5]<div>字符串0</div>
         [6]
         [7]
         [8]
         [9]
    <h4>二、switch case</h4>
        <select>
            <option></option>
            
            
        </select>
    
        <select>
            
            
            <option>其他</option>
        </select>
    
    </body>
    </html>
  • 相关阅读:
    【java】jfairy和java-faker假数据利器
    【Spring boot】【gradle】idea新建spring boot+gradle项目
    【mac】mac上使用brew 安装速度慢/每次使用brew 都会卡在updating homebrew不动/更换homebrew的镜像源
    【gradle】mac上安装gradle
    【mac】mac上安装JDK
    如何解决ajax跨域问题(转)
    java实现点选汉字验证码(自己修改后的)
    AES加解密
    java随机打乱集合顺序
    利用StringEscapeUtils对字符串进行各种转义与反转义(Java)
  • 原文地址:https://www.cnblogs.com/gdjlc/p/11695517.html
Copyright © 2020-2023  润新知