Thymeleaf common syntax: conditional judgment if, switch case

The if statement
uses th:if to determine whether the expression is true. The result of the expression supports boolean, number, character, String and other types.
The if statement is true when the following conditions are met:
(1) The result of the expression is a number and not 0
(2) The result of the expression is a string and not false, off, no, 0
(3) The result of the expression is other data Type
switch case statement
(1) Similar to Java's switch case statement: th:switch, th:case
(2) Use th:case="*" to indicate the default value
(3) If the first th:case result is true, Then all other th:cases will be set to false, even if their results are also true

Development environment: IntelliJ IDEA 2019.2.2
Spring Boot version: 2.1.8

Create a new Spring Boot project named demo.

1.
Add the Thymeleaf dependency to pom.xml

        <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>

Browser access: http://localhost:8080,
right click to view the source code of the webpage, the generated HTML source code:

<!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>

 

 

Guess you like

Origin blog.csdn.net/gdjlc/article/details/102616379