Thymeleaf common syntax: data delayed loading

When the processing template, the template may decide whether the logic load data, in order to improve performance.
Spring Boot controller when the setting data, this function may be implemented using LazyContextVariable.

Development Environment: IntelliJ IDEA 2019.2.2
the Spring the Boot Version: 2.1.8

Create a new name for the demo of Spring Boot project.

1, pom.xml
added Thymeleaf dependent

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

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

package com.example.demo;

public class User {
    Integer id;
    String name;

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

3、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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.thymeleaf.context.LazyContextVariable;

import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {
    @RequestMapping("/{show}")
    public String test(Model model, @PathVariable("show") boolean show){
        model.addAttribute ( "Users", new new LazyContextVariable () { 
            @Override 
            protected Object loadValue () {
                 return queryUsers (); 
            } 
        }); 
        model.addAttribute ( "Show" , Show);
         return "Test" ; 
    } 

    Private List < the User> queryUsers () { 
        System.out.println ( "analog data query, practical applications may directly query the database" ); 
        List <the User> Users = new new the ArrayList <the User> (); 
        users.add ( new new the User (. 1, "John Doe")); 
        users.add(new User(2,"李四"));
        users.add(new User(3,"王五"));
        return users;
    }
}

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        table { border-collapse:collapse;}
        td { border: 1px solid #C1DAD7;}
    </style>
</head>
<body>
    <table th:if="${show == true}">
        <tr th:each="user : ${users}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}"></td>
        </tr>
    </table>
</body>
</html>

 

Browser access:
HTTP: // localhost: 8080 / false, the page did not display the data, not the console output.
http: // localhost: 8080 / true , the page display data, the console output "Analog query data, practical applications can query the database directly."

Guess you like

Origin www.cnblogs.com/gdjlc/p/11701297.html