广告
返回顶部
首页 > 资讯 > 前端开发 > VUE >Thymeleaf数据延迟加载怎么实现
  • 438
分享到

Thymeleaf数据延迟加载怎么实现

2024-04-02 19:04:59 438人浏览 独家记忆
摘要

这篇文章主要介绍“Thymeleaf数据延迟加载怎么实现”,在日常操作中,相信很多人在Thymeleaf数据延迟加载怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Th

这篇文章主要介绍“Thymeleaf数据延迟加载怎么实现”,在日常操作中,相信很多人在Thymeleaf数据延迟加载怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Thymeleaf数据延迟加载怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

在处理模板时,可以由模板逻辑决定是否加载数据,以提高性能。
Spring Boot控制器中设置数据时,使用LazyContextVariable可以实现这功能。

开发环境: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/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 LazyContextVariable() {
            @Override
            protected Object loadValue() {
                return queryUsers();
            }
        });
        model.addAttribute("show", show);
        return "test";
    }

    private List<User> queryUsers(){
        System.out.println("模拟查询数据,实际应用中可以直接查询数据库");
        List<User> users = new ArrayList<User>();
        users.add(new User(1,"张三"));
        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>

浏览器访问:
Http://localhost:8080/false ,页面没显示数据,控制台没输出信息。
http://localhost:8080/true ,页面显示数据,控制台输出"模拟查询数据,实际应用中可以直接查询数据库”。

到此,关于“Thymeleaf数据延迟加载怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: Thymeleaf数据延迟加载怎么实现

本文链接: https://www.lsjlt.com/news/88598.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
推荐阅读
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作