Sintaxis común de Thymeleaf: la expresión en el archivo de plantilla llama al método estático de la clase Java

En la expresión del archivo de plantilla, puede utilizar el formato "$ {T (nombre de clase totalmente calificado). Nombre de método (parámetro)}" para llamar al método estático de la clase Java.

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 / TestUtils.java

package com.example.demo;

public class TestUtils {
    public static String toUpperCase(String s){
        return s.toUpperCase();
    }
}

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

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping("/")
    public String test(){
        return "test";
    }

    public static String toLowerCase(String s){
        return s.toLowerCase();
    }
}

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

<div th:text="${T(com.example.demo.TestUtils).toUpperCase('hello world 1')}"></div>
<div th:text="${T(com.example.demo.TestController).toLowerCase('HELLO WORLD 2')}"></div> 

Acceso al navegador: http: // localhost:
salida de la página 8080 :
 

HELLO WORLD 1
hello world 2

 

Supongo que te gusta

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