簡單了解Thymeleaf語法 數(shù)據(jù)延遲加載使用實(shí)例
在處理模板時(shí),可以由模板邏輯決定是否加載數(shù)據(jù),以提高性能。
在Spring Boot控制器中設(shè)置數(shù)據(jù)時(shí),使用LazyContextVariable可以實(shí)現(xiàn)這功能。
開發(fā)環(huán)境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8
新建一個(gè)名稱為demo的Spring Boot項(xiàng)目。
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("模擬查詢數(shù)據(jù),實(shí)際應(yīng)用中可以直接查詢數(shù)據(jù)庫");
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 ,頁面沒顯示數(shù)據(jù),控制臺沒輸出信息。
http://localhost:8080/true ,頁面顯示數(shù)據(jù),控制臺輸出"模擬查詢數(shù)據(jù),實(shí)際應(yīng)用中可以直接查詢數(shù)據(jù)庫”。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis實(shí)現(xiàn)一對一關(guān)聯(lián)映射實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于mybatis實(shí)現(xiàn)一對一關(guān)聯(lián)映射的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway詳細(xì)分析
這篇文章主要介紹了SpringCloud實(shí)現(xiàn)服務(wù)調(diào)用feign與熔斷hystrix和網(wǎng)關(guān)gateway,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-04-04
Spring動(dòng)態(tài)監(jiān)聽Nacos配置中心key值變更的實(shí)現(xiàn)方法
Nacos本身提供支持監(jiān)聽配置變更的操作,但在使用起來,個(gè)人感覺不是很友好,無法精確到某個(gè)key的變更監(jiān)聽,所以本文小編給大家介紹了Spring動(dòng)態(tài)監(jiān)聽Nacos配置中心key值變更的實(shí)現(xiàn)方法,需要的朋友可以參考下2024-08-08
Java實(shí)現(xiàn)生成二維碼展示到瀏覽器的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)生成二維碼展示到瀏覽器的示例代碼,要實(shí)現(xiàn)在瀏覽器展示二維碼,那么首先需要html文件,通過Java生成二維碼的工具類,在controller層調(diào)用接口,就可以實(shí)現(xiàn)在瀏覽器上展示二維碼,需要的朋友可以參考下2024-01-01

