The original article was published on wechat official account: Jzman-blog

Thymeleaf is a Java template engine for Web development that can handle HTML, XML, JavaScript, CSS, and even plain text. Spring Boot recommends using Thymeleaf instead of traditional JSP technology. The main contents are as follows:

  1. Introducing the Thymeleaf
  2. Thymeleaf properties
  3. The use of the Thymeleaf
  4. Hot deployment

Introducing the Thymeleaf

Gradle is used to build your entire Web project. Build. Gradle file with Thymeleaf dependencies as follows:

dependencies {
    // Introduce the thymeleaf dependency
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}
Copy the code

Because Thymeleaf is a third-party plugin, you need to specify the corresponding classpath in the build.gradle file as follows:

// Third-party plug-ins need to specify the corresponding classpath
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath("Org. Springframework. The boot: spring - the boot - gradle - plugin: 2.2.5. RELEASE")}}Copy the code

At this point Thymeleaf has been imported into the Web project, and you can check the list of imported libraries to see if Thymeleaf has been imported correctly.

Thymeleaf properties

# Enable template caching, default true spring.thymeleaf.cache=false # Check for template presence before rendering #spring.thymeleaf.check-template=true # Check for template location Check -template-location=true # Whether to enable the SpringEL compiler in SpringEL expressions Enable-spring-el-compiler =false # Whether to enable Thymeleaf view parsing for Web frameworks #spring.thymeleaf.enabled=true # Template encoding # spring. Thymeleaf. Encoding = utf-8 # should be eliminated from the name of the view from the solution of a comma-separated list # spring. The thymeleaf. Excluded - view - names # template pattern #spring. Thymeleaf. mode=HTML #spring. Thymeleaf. prefix=classpath:/templates/ #spring.thymeleaf.suffix=.html # comma-separated list of view names, when the maximum block size is set, Should be the only of executing, CHUNKED view name list # spring. The thymeleaf. Reactive. CHUNKED - mode - view - # names if set up the large size, Should be under the FULL mode of comma-separated list of view name # spring. The thymeleaf. Reactive. FULL - mode - view - # names for writing response of the maximum size of the data buffer, if set the template, is by default will be executed in CHUNKED mode # spring. Thymeleaf. Reactive. Max -- the chunk size = 0 b # # view technical support of media types spring. The thymeleaf. Reactive. The media types - # been hidden form inputs acting as markers for checkboxes should be rendered before the checkbox element itself. #spring.thymeleaf. Render - hiddle-markers -before-checkboxes=false # Content-type Type of the HTTP response # spring. Thymeleaf. Servlet. The content-type = text/HTML # thymeleaf should as far as possible or written to the output buffer until template processing is completed #spring.thymeleaf.servlet.produce-partial-output-while-processing=true # Template parser in the order of the chain, by default, the template parser is located in the chain of the first, starting from 1, and only in the case of other TemplateResolver defines to set # spring. The thymeleaf. Template - resolver - order # A comma-separated list of resolvable view names #spring.thymeleaf.view-namesCopy the code

The use of the Thymeleaf

After importing the Thymeleaf dependency successfully, create the template file hello.html under Resources /templates as follows:

<! DOCTYPEhtml>
<! Thymeleaf namespace must be added.
<html lang="en" xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <title>thymeleaf</title>
</head>

<body>
    <p th:text="${name}">Hello World!</p>
</body>

</html>
Copy the code

The Thymeleaf tag in the above code is used in the Html tag, which is different from other template engines. ${name} will return the value of name, and when processing the template will replace the contents of the P tag with the value of name. Create the corresponding Controller as follows:

@Controller
public class ThymeleafController {
    @RequestMapping("/index")
    public String hello(Model model){
        model.addAttribute("name"."jzman");
        return "hello"; }}Copy the code

After running, you can access the following address:

http://localhost:8080/index
Copy the code

The running results are as follows:

jzman
Copy the code

Hot deployment

Add devTools to build.gradle as follows:

dependencies {
    / / hot deployment
    implementation("Org. Springframework. The boot: spring - the boot - devtools: 2.0.2. RELEASE")}Copy the code

Then, press Ctrl+Shift+A to find Registry and check the following option:

Finally, check the following option in the setup Compiler:

After the configuration is complete, the Thymeleaf template cache should be disabled to ensure timely updates:

spring.thymeleaf.cache=false
Copy the code

After running the project, if the project changes, you can use the shortcut key Ctrl+F9 rapid deployment, can pay attention to the public number [practice] exchange learning, reply keyword [Spring Boot] to obtain the corresponding case source link.