Sintaxis común de Thymeleaf: juicio condicional si, cambiar mayúsculas y minúsculas

La instrucción if
usa th: if para determinar si la expresión es verdadera. El resultado de la expresión admite booleano, número, carácter, cadena y otros tipos.
La declaración if es verdadera cuando se cumplen las siguientes condiciones:
(1) El resultado de la expresión es un número y no 0
(2) El resultado de la expresión es una cadena y no falso, desactivado, no, 0
(3) El el resultado de la expresión es otro tipo de datos
instrucción switch case
(1) Similar a la instrucción switch case de Java: th: switch, th: case
(2) Use th: case = "*" para indicar el valor predeterminado
(3) Si el primer th: case el resultado es verdadero, entonces todos los demás th: cases se establecerán en falso, incluso si sus resultados también son verdaderos

Entorno de desarrollo: IntelliJ IDEA 2019.2.2
Spring Boot versión: 2.1.8

Cree un nuevo proyecto de Spring Boot llamado demo.

1.
Agregue la dependencia Thymeleaf a 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>

Acceso al navegador: http: // localhost: 8080,
haga clic derecho para ver el código fuente de la página web, el código fuente HTML generado:

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

 

 

Supongo que te gusta

Origin blog.csdn.net/gdjlc/article/details/102616379
Recomendado
Clasificación