Spring-boot-thymeleaf

提供:Dev Guides
移動先:案内検索

春のブーツ-タイムリーフ

Thymeleafは、Webアプリケーションの作成に使用されるJavaベースのライブラリです。 WebアプリケーションでXHTML/HTML5を提供するための優れたサポートを提供します。 この章では、Thymeleafについて詳しく学習します。

Thymeleafテンプレート

Thymeleafは、ファイルを整形式のXMLファイルに変換します。 それは以下に示すようにテンプレートの6種類が含まれています-

  • XML
  • 有効なXML
  • XHTML
  • 有効なXHTML
  • HTML5
  • レガシーHTML5

レガシーHTML5を除くすべてのテンプレートは、整形式の有効なXMLファイルを参照しています。 レガシーHTML5では、閉じていないタグを含むWebページでHTML5タグをレンダリングできます。

ウェブアプリケーション

Thymeleafテンプレートを使用して、Spring BootでWebアプリケーションを作成できます。 Thymeleafを使用してSpring BootでWebアプリケーションを作成するには、次の手順に従う必要があります。

次のコードを使用して、リクエストURIをHTMLファイルにリダイレクトする@Controllerクラスファイルを作成します-

package com.finddevguides.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class WebController {
   @RequestMapping(value = "/index")
   public String index() {
      return "index";
   }
}

上記の例では、リクエストURIは /index であり、コントロールはindexlファイルにリダイレクトされます。 indexlファイルはテンプレートディレクトリの下に配置し、すべてのJSおよびCSSファイルはクラスパスの静的ディレクトリの下に配置する必要があります。 示された例では、CSSファイルを使用してテキストの色を変更しました。

次のコードを使用して、別のフォルダー css にCSSファイルを作成し、ファイルにstyles.cssという名前を付けることができます-

h4 {
   color: red;
}

indexlファイルのコードを以下に示します-

<!DOCTYPE html>
<html>
   <head>
      <meta charset = "ISO-8859-1"/>
      <link href = "css/styles.css" rel = "stylesheet"/>
      <title>Spring Boot Application</title>
   </head>
   <body>
      <h4>Welcome to Thymeleaf Spring Boot web application</h4>
   </body>
</html>

プロジェクトエクスプローラは、以下のスクリーンショットに示されています-

プロジェクトエクスプローラーのスクリーンショット

ここで、Spring Boot Starter Thymeleaf依存関係をビルド構成ファイルに追加する必要があります。

Mavenユーザーは、pom.xmlファイルに次の依存関係を追加できます-

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

Gradleユーザーは、build.gradleファイルに次の依存関係を追加できます-

compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'

メインのSpring Bootアプリケーションクラスファイルのコードは以下のとおりです-

package com.finddevguides.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {
   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }
}

Mavenのコード– pom.xmlは以下のとおりです-

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>
   <groupId>com.finddevguides</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>
   <name>demo</name>
   <description>Demo project for Spring Boot</description>

   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.8.RELEASE</version>
      <relativePath/>
   </parent>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
      <java.version>1.8</java.version>
   </properties>

   <dependencies>
      <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>

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

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

Gradleのコード-build.gradleは以下のとおりです-

buildscript {
   ext {
      springBootVersion = '1.5.8.RELEASE'
   }
   repositories {
      mavenCentral()
   }
   dependencies {
      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
   }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

group = 'com.finddevguides'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
dependencies {
   compile('org.springframework.boot:spring-boot-starter-web')
   compile group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf'
   testCompile('org.springframework.boot:spring-boot-starter-test')
}

次のMavenまたはGradleコマンドを使用して、実行可能なJARファイルを作成し、スプリングブートアプリケーションを実行できます-

Mavenの場合、以下に示すようにコマンドを使用します-

mvn clean install

「BUILD SUCCESS」の後、ターゲットディレクトリの下にJARファイルがあります。

Gradleの場合、以下に示すようにコマンドを使用します-

gradle clean build

「BUILD SUCCESSFUL」の後、build/libsディレクトリの下にJARファイルがあります。

ここで指定されたコマンドを使用してJARファイルを実行します-

java –jar <JARFILE>

これで、アプリケーションは以下に示すようにTomcatポート8080で開始されました-

Tomcat Port_8080で開始されたアプリケーション

今、あなたのウェブブラウザでURLを打つと、次のように出力を見ることができます-

*http://localhost:8080/index*

Spring Boot Thymleaf Webアプリケーション