SpringBoot2.1.5(33)--- SpringBoot統合Thymeleafのテンプレートエンジン

 

ディレクトリ

 

、Thymeleaf

要約:

Webおよび独立した環境のためのThymeleaf近代的なサーバーサイドJavaのテンプレートエンジン。

主な目的は、ブラウザで正しく表示さ-html開発のワークフローの自然な優雅にThymeleafのテンプレートになり、開発チーム全体でより強力なコラボレーションを実現するためには、静的なプロトタイプとして使用することができます。ThymeleafはHTML、XML、JavaScriptやCSS、あるいはプレーンテキストを扱うことができます。

Thymeleaf主な目標は、テンプレートを作成するには、エレガントで、非常に保守方法を提供することです。これを達成するために、それは自然なテンプレートの概念に基づいて構築され、テンプレートは、そのロジックはプロトタイプを設計するための方法として、テンプレートファイル内に注入される影響はありません。これは、フロントエンドの設計と開発スタッフとの間の差を埋めるために設計コミュニケーションと理解を向上させます。

Thymeleafは最初から設計されています(特にHTML5)は、あなたが完全に検証テンプレートを作成することができます。

公式サイト

https://www.thymeleaf.org/

公式ドキュメント

https://www.thymeleaf.org/documentation.html

入門情報リファレンス:

https://blog.csdn.net/zrk1000/article/details/72667478

第二に、コードの練習

1、Mavenの依存性

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2、YMLの設定ファイル

server:
  port: 8081
  
spring:
  application:
    name: thymeleaf

  thymeleaf:
    # 是否启用模板缓存。
    cache: true
    # 是否检查模板位置是否存在。
    check-template: true
    # 是否为Web框架启用Thymeleaf视图分辨率。
    enabled: true
    # 编码格式, 默认UTF-8
    encoding: UTF-8
    # 应用于模板的模板模式。另请参阅Thymeleaf的TemplateMode枚举。
    mode: HTML
    # 后缀 默认 .html
    suffix: .html
    # 模板文件存放位置  , 默认 classpath:/templates/
    prefix: classpath:/templates/

3、Thymeleafファイル

次のようにテンプレートのindex.htmlファイルを作成し、文書を読み込み、

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
   <h1 th:text="${content}"></h1>
</body>
</html>

4.テストコントローラ

/**
 * created with IntelliJ IDEA.
 * author: fxbin
 * date: 2018/10/21
 * time: 4:42
 * version: 1.0
 * description:
 */
@Controller
public class ThymeleafController {

    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("content", "Hello Thymeleaf");
        return "index";
    }
}

5、プロジェクト、テストを開始

アクセス  HTTP:// localhostを:8081 /テスト  、我々はコントローラに提供されたコンテンツが正常にページ上に表示されていることが分かります
ここに画像を挿入説明

 

 

おすすめ

転載: blog.csdn.net/zhangbijun1230/article/details/91357519