zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    sshd
    eclipse运行报java.lang.OutOfMemoryError: PermGen space解决方法
    项目之间依赖
    shell-
    部署记录
    mysql index使用
    GitHub上搭建私人hexo博客操作教程
    关于Vue实例的生命周期(2)
    JS中的五种去重方法
    Vue入门教程(2)
  • 原文地址:https://www.cnblogs.com/gdjlc/p/11695517.html
Copyright © 2011-2022 走看看