Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article is not the author of the self-regulation of the program ape original, please reprint the private letter and at the beginning of the article with the author and the original address link.
Lao homely
This article is the pilot of the Thymeleaf series, which will introduce Thymeleaf. The problems and solutions encountered in learning and using Thymeleaf will be updated continuously.
Don’t say a word, just turn it on!
Here are some of the sites on Thymeleaf’s website.
- Thymeleaf website
- Thymeleaf reference documentation
- Thymeleaf API
Thymeleaf introduction
Quote from the official website
Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
Thymeleaf’s main goal is to bring elegant natural templates to your development workflow — HTML that can be correctly displayed in browsers and also work as static prototypes, allowing for stronger collaboration in development teams.
With modules for Spring Framework, a host of integrations with your favourite tools, and the ability to plug in your own functionality, Thymeleaf is ideal for modern-day HTML5 JVM Web Development — although there is much more it can do.
Translation:
Thymeleaf is a modern server-side Java templating engine for Web and standalone environments.
Thymeleaf’s primary goal is to bring elegant natural templates to your development workflow — HTML that displays correctly in a browser or works as a static prototype, enhancing collaboration among development teams.
With the Spring Framework’s modules, extensive integration with your favorite tools, and the ability to plug in your own functionality, Thymeleaf is ideal for modern HTML5 JVM Web development — though it has more.
At this point, the question arises, why do we use Thymeleaf?
If you want to publish your modules as JARS, try not to use JSP knowledge because JSPS have some problems running JSPS on embedded Servlet containers (embedded Tomcat, Jetty does not support JSPS running in Jar form, and Undertow does not support JSPS). Thymeleaf is recommended as a template engine in Spring Boot because Thymeleaf provides perfect Spring MVC support. Spring Boot also provides a number of template engines, including:
- FreeMarker
- Groovy
- Mustache
- Thymeleaf
- Velocity
- Beetl
Can Thymeleaf completely replace JSP and JSTL? The response to this question is absolutely acceptable and highly recommended. And also gives an article, the two technologies were compared,Thymeleaf and JSP, interested partners can check.
SpringBoot integration Thymeleaf
Maven rely on
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Copy the code
There are two configuration modes, one is properties mode, the other is YML, choose one by yourself
- application.properties
It is recommended that thymeleaf cache be turned off during development
spring.thymeleaf.cache=false
# Use legacy HTML5 to remove validation of HTML tags
spring.thymeleaf.mode=LEGACYHTML5
Copy the code
- application.yml
spring:
thymeleaf:
cache: false
mode: LEGACYHTML5
Copy the code
IndexController
@Controller
@RequestMapping("/index")
public class IndexController {
@RequestMapping("/home")
public String hello(Model model) {
model.addAttribute("msg"."A less self-disciplined programmer.");
return "test"; }}Copy the code
In the SRC/main/resources/templates to create a template file below home. HTML
File path: home.html
<! DOCTYPE HTML > < HTML XMLNS :th="http://www.thymeleaf.org"> <head> <meta charset=" utF-8 "> <title> </head> <body> <span th:text="${MSG}">Copy the code
Start the program, visit http://localhost:8080/index/home.
The follow-up will continue to update, interested partners can continue to pay attention to!
Thank you for watching, if there is a mistake in the article, welcome to the comment section to communicate. If this article has helped you, please like it at 👍.