Spring Boot系列之Thymeleaf模板入门

原文首发于微信公众号:躬行之(jzman-blog)

Thymeleaf 是一个用于 Web 开发的 Java 模板引擎,能够处理 HTML、XML、JavaScript、CSS 甚至纯文本,Spring Boot 推荐使用 Thymeleaf 模板引擎而不是传统的 JSP 技术,主要内容如下:

  1. 引入Thymeleaf
  2. Thymeleaf属性
  3. Thymeleaf的使用
  4. 热部署

引入Thymeleaf

个人觉得 Gradle 相较 Maven 更简洁,这里是用 gradle 来构建整个 Web 项目,在 build.gradle 文件中引入 Thymeleaf 依赖如下:

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

因为 Thymeleaf 属于第三方插件,还需在在 build.gradle 文件中指定对应的 classpath ,在 build.gradle 文件中配置如下:

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

至此 Thymeleaf 引入到 Web 项目中了,可在导入的库列表中查看 Thymeleaf 是否正确导入。

Thymeleaf属性

# 启用模板缓存,默认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

Thymeleaf的使用

引入 Thymeleaf 依赖成功之后,在 resources/templates 下面创建模板文件 hello.html 如下:

<!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 的标签都在 Html 标签中使用,这也是有别于其他模板引擎的地方,${name} 会回去 name 的值在处理模板的时候会将 p 标签中的内容替换成 name 的值,然后,创建对应的 Controller 如下:

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

运行之后就可以在访问如下地址:

http://localhost:8080/index

其运行结果如下:

jzman

热部署

在 build.gradle 文件中引入 devtools 如下:

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

然后,按 Ctrl+Shift+A,找到 Registry 勾选下面这个选项:

最后,在设置 Compiler 中勾选下面这个选项:

配置完成后,为了保证能够及时更新,应该禁用 Thymeleaf 模板缓存:

spring.thymeleaf.cache=false

运行项目之后,如果项目有所变动,可以使用快捷键 Ctrl+F9 快速部署,可关注公众号【躬行之】交流学习。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jzman/article/details/109302564