SpringBootシリーズのThymeleafテンプレートの使用を開始する

元のテキストは、WeChatパブリックアカウントで最初に公開されました:Jongxingzhi(jzman-blog)

Thymeleafは、HTML、XML、JavaScript、CSS、さらにはプレーンテキストを処理できるWeb開発用のJavaテンプレートエンジンです。SpringBootでは、従来のJSPテクノロジーの代わりにThymeleafテンプレートエンジンを使用することをお勧めします。主な内容は次のとおりです。

  1. Thymeleafを紹介します
  2. Thymeleaf属性
  3. Thymeleafの使用
  4. ホット展開

Thymeleafを紹介します

個人的には、GradleはMavenよりも簡潔だと感じています。これは、Gradleを使用してWebプロジェクト全体をビルドする方法です。Thymeleaf依存関係は、build.gradleファイルに次のように導入されています。

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

Thymeleafはサードパーティのプラグインであるため、build.gradleファイルで対応するクラスパスを指定し、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}プロセステンプレートの名前の値がpタグの内容を値の名前に置き換えるときに戻り、対応するコントローラーは次のとおりです。

@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を押し、レジストリを見つけて、次のオプションを確認します。

最後に、コンパイラ設定で次のオプションを確認します。

構成が完了したら、タイムリーな更新を確実にするために、Thymeleafテンプレートのキャッシュを無効にする必要があります。

spring.thymeleaf.cache=false

プロジェクトの実行後、プロジェクトが変更された場合は、ショートカットキーCtrl + F9を使用してすばやく展開でき、公式アカウント[Jingxingzhi]に従って交換と学習を行うことができます。

ここに画像の説明を挿入します

おすすめ

転載: blog.csdn.net/jzman/article/details/109302564