Getting started with Thymeleaf template of Spring Boot series

The original text was first published on WeChat public account: Jongxingzhi (jzman-blog)

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

  1. Introduce Thymeleaf
  2. Thymeleaf attributes
  3. Use of Thymeleaf
  4. Hot deployment

Introduce Thymeleaf

I personally feel that Gradle is more concise than Maven. Here is the use of gradle to build the entire Web project. The Thymeleaf dependency is introduced in the build.gradle file as follows:

dependencies {
    
    
    // 引入thymeleaf依赖
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
}

Because Thymeleaf is a third-party plug-in, you also need to specify the corresponding classpath in the build.gradle file, and configure it as follows in the build.gradle file:

// 第三方的插件需要指定对应的classpath
buildscript {
    
    
    repositories {
    
    
        jcenter()
    }
    dependencies {
    
    
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.5.RELEASE")
    }
}

So far Thymeleaf has been introduced into the Web project. You can check whether Thymeleaf is imported correctly in the list of imported libraries.

Thymeleaf attributes

# 启用模板缓存,默认true
spring.thymeleaf.cache=false
# 在渲染之前检查模板是否存在
#spring.thymeleaf.check-template=true
# 检查模板位置是否存在
#spring.thymeleaf.check-template-location=true
# 是否在SpringEL表达式中启用SpringEL编译器
#spring.thymeleaf.enable-spring-el-compiler=false
# 是否为Web框架启用Thymeleaf视图解析
#spring.thymeleaf.enabled=true
# 模板编码
#spring.thymeleaf.encoding=UTF-8
# 应该从解决方案中排除的视图名称的逗号分隔列表
#spring.thymeleaf.excluded-view-names
# 模板模式
#spring.thymeleaf.mode=HTML
# 构建URL时的前缀
#spring.thymeleaf.prefix=classpath:/templates/
# 构建URL时的后缀
#spring.thymeleaf.suffix=.html
# 逗号分隔的视图名称列表,当设置了最大块大小时,应该是CHUNKED模式中唯一执行的视图名称列表
#spring.thymeleaf.reactive.chunked-mode-view-names
# 如果设置了最大块的大小,应该是FULL模式下的逗号分隔的视图名称列表
#spring.thymeleaf.reactive.full-mode-view-names
# 用于写入响应的数据缓冲区的最大大小,如果设置了模板,则默认情况下将以CHUNKED模式执行
#spring.thymeleaf.reactive.max-chunk-size=0B
# 视图技术支持的媒体类型
#spring.thymeleaf.reactive.media-types
#Whether hidden form inputs acting as markers for checkboxes should be rendered before the checkbox element itself.
#spring.thymeleaf.render-hidden-markers-before-checkboxes=false
# HTTP响应的Content-Type类型
#spring.thymeleaf.servlet.content-type=text/html
# Thymeleaf应该尽可能的写入输出或者缓冲直到模板处理完成
#spring.thymeleaf.servlet.produce-partial-output-while-processing=true
# 模板解析器在链中的顺序,默认情况下,模板解析器位于链中的第一位,1开始,并且仅在定义了其他TemplateResolver的情况下才能设置
#spring.thymeleaf.template-resolver-order
# 可以解析的视图名称的逗号分隔列表
#spring.thymeleaf.view-names

Use of Thymeleaf

After successfully introducing the Thymeleaf dependency, create a template file hello.html under resources/templates as follows:

<!DOCTYPE html>
<!--必须加入thymeleaf的命名空间-->
<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>

Thymeleaf above code labels are used in the Html tags, which is different from other template engines place, ${name}we will go back when the value of the name of the process template will preplace the contents of the tag to a value name, and then creates a corresponding The Controller is as follows:

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

After running, you can visit the following address:

http://localhost:8080/index

The results of its operation are as follows:

jzman

Hot deployment

Introduce devtools in the build.gradle file as follows:

dependencies {
    
    
    // 热部署
    implementation("org.springframework.boot:spring-boot-devtools:2.0.2.RELEASE")
}

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

Finally, check the following option in the Compiler settings:

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

spring.thymeleaf.cache=false

After running the project, if the project has changed, you can use the shortcut key Ctrl+F9 to quickly deploy, and you can follow the official account [Jingxingzhi] for exchange and learning.

Insert picture description here

Guess you like

Origin blog.csdn.net/jzman/article/details/109302564