Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize delayed loading of Thymeleaf data

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to achieve delayed loading of Thymeleaf data". In daily operation, I believe that many people have doubts about how to achieve delayed loading of Thymeleaf data. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to achieve delayed loading of Thymeleaf data". Next, please follow the editor to study!

When working with templates, it is up to the template logic to decide whether or not to load data to improve performance.

You can do this using LazyContextVariable when setting up data in the Spring Boot controller.

Development environment: IntelliJ IDEA 2019.2.2

Spring Boot version: 2.1.8

Create a new Spring Boot project named demo.

1 、 pom.xml

Join Thymeleaf dependency

Org.springframework.boot spring-boot-starter-thymeleaf

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 @ Controllerpublic 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 queryUsers () {System.out.println ("simulated query data, but database can be queried directly in practical applications"); List users = new ArrayList (); users.add (new User (1, "Zhang San")); users.add (new User (2, "Li Si")); users.add (new User (3, Wang Wu)); return users;}}

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

Title table {border-collapse:collapse;} td {border: 1px solid # C1DAD7;}

Browser access:

Http://localhost:8080/false, the page does not display data, and the console does not output information.

Http://localhost:8080/true, page display data, console output "simulated query data, practical application can directly query the database".

At this point, the study on "how to achieve delayed loading of Thymeleaf data" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report