Today I learned about the Spring boot Integrated thymeleaf template engine. Discover that the Thymeleaf function is really powerful. This is recorded for later use.
thymeleaf:
Thymeleaf is a Java class library, a XML/XHTML/HTML5 template engine that can be used as the view layer for MVC Web applications.
Thymeleaf also provides additional modules for integration with spring MVC, so we can use Thymeleaf to completely replace the JSP. The
Spring Boot
is configured automatically via the Org.springframework.boot.autoconfigure.thymeleaf package for Thymeleaf. The
Thymeleafautoconfiguration class is used to automatically configure the beans needed for integration. Includes the Templateresolver,templateengine,thymeleafviewresolver configuration.
I'll show you the thymeleaf usage that's commonly used in spring boot routines. usage of common thymeleaf in Spring Boot routine
1: First, select Thymeleaf in the dependency selection when creating the project, or add dependencies to the POM
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId> Spring-boot-starter-thymeleaf</artifactid>
</dependency>
or the project name-right--add Framework support to add a dependent jar package. As shown in figure
2: Example JavaBean
This class is used to display data in a template page. Contains the name and age attributes.
public class Person {
private String name;
Private Integer age;
Public person (String name, Integer age) {
this.name = name;
This.age = age;
}
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
Public Integer Getage () {return age
;
}
public void Setage (Integer age) {
this.age = age;
}
}
3. Script-style static file
According to the default rules, script styles, pictures, and other static files should be placed under Src/main/resources/static, where Bootstrap and jquery are introduced, as shown in the diagram:
4. Demo page
According to the default principle, the page should be placed under src/main/resources/templates. Create a new index.html below the Src/main/resources/templates, as shown above.
The code is as follows:
<! DOCTYPE html>
5. Data preparation
code is as follows:
Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RestController;
Import java.util.ArrayList;
Import java.util.List; @Controller @SpringBootApplication public class Thymeleaftestapplication {@RequestMapping ("/") public String Inde
X (model model) {person single=new person ("AA", 1);
List<person> people=new arraylist<person> ();
Person p1=new person ("BB", 2);
Person P2=new person ("CC", 3);
Person P3=new person ("DD", 4);
People.add (p1);
People.add (p2);
People.add (p3);
Model.addattribute ("Singleperson", single);
Model.addattribute ("People", people);
Return "index"; public static void Main (string[] args) {SprinGapplication.run (Thymeleaftestapplication.class, args);
}
}
6. Run
Access http://localhost:8080 effect figure:
Click "Get First Name" F12 see the page console print log effects as shown: