新西兰服务器

Thymeleaf数据延迟加载怎么实现


Thymeleaf数据延迟加载怎么实现

发布时间:2022-01-21 19:08:52 来源:高防服务器网 阅读:92 作者:iii 栏目:web开发

这篇文章主要介绍“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数据延迟加载怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注高防服务器网网站,小编会继续努力为大家带来更多实用的文章!

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[