简单了解Thymeleaf语法 数据延迟加载使用实例
作者:gdjlc 时间:2021-11-27 04:05:49
在处理模板时,可以由模板逻辑决定是否加载数据,以提高性能。
在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,数据,延迟,加载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot配置SwaggerUI访问404错误的解决方法
2021-10-02 19:33:56
![](https://img.aspxhome.com/file/2023/3/68743_0s.png)
使用SpringMVC访问Controller接口返回400BadRequest
2023-06-05 00:14:01
Java 使用 HttpClient 发送 GET请求和 POST请求
2023-07-23 07:56:13
Java如何将ResultSet结果集遍历到List中
2022-07-01 06:30:46
![](https://img.aspxhome.com/file/2023/7/83097_0s.jpg)
Android 简单实现倒计时功能
2023-06-27 11:35:40
![](https://img.aspxhome.com/file/2023/5/91035_0s.jpg)
Java 继承与多态的深入理解
2023-10-05 04:25:41
android 6.0 写入SD卡的权限申请实例讲解
2023-07-27 03:12:37
c# SendMail发送邮件实例代码
2023-03-13 11:17:21
java compare compareTo方法区别详解
2022-06-26 08:13:55
Java虚拟机GC日志分析
2023-02-21 15:04:40
Jsoup获取全国地区数据属性值(省市县镇村)
2023-12-08 01:27:25
![](https://img.aspxhome.com/file/2023/8/86728_0s.png)
使用Filter过滤器中访问getSession()要转化
2022-10-01 16:20:04
c# 死锁和活锁的发生及避免
2023-05-28 00:45:49
![](https://img.aspxhome.com/file/2023/7/76427_0s.png)
java 抛出异常处理的方法
2023-05-12 21:55:54
SpringBoot如何使用RateLimiter通过AOP方式进行限流
2023-09-16 18:18:44
![](https://img.aspxhome.com/file/2023/3/58583_0s.png)
Java实现五子棋游戏的完整代码
2022-07-01 15:32:34
![](https://img.aspxhome.com/file/2023/8/62328_0s.jpg)
Flask实现异步非阻塞请求功能实例解析
2022-02-03 14:03:55
SpringBoot项目中新增脱敏功能的实例代码
2023-11-24 01:32:14
什么是递归?用Java写一个简单的递归程序
2022-02-11 19:39:45
![](https://img.aspxhome.com/file/2023/2/60772_0s.jpg)
详解springboot读取yml配置的几种方式
2023-08-21 02:44:54