Skip to content
Server Plugin

Thymeleaf

必要的相依性:io.ktor:ktor-server-thymeleaf

程式碼範例: thymeleaf

原生伺服器Ktor 支援 Kotlin/Native,可讓您在無需額外執行階段或虛擬機的情況下執行伺服器。 支援:✖️

Ktor 允許你透過安裝 Thymeleaf 外掛程式,在應用程式中將 Thymeleaf 範本 作為檢視使用。

新增相依性 ​

若要使用 Thymeleaf,請將 ktor-server-thymeleaf 構件新增至你的建置指令碼中:

kotlin
implementation(ktorLibs.server.thymeleaf)
上述程式碼片段使用 Ktor 版本目錄 來指定 Ktor 相依性。
groovy
implementation ktorLibs.server.thymeleaf
上述程式碼片段使用 Ktor 版本目錄 來指定 Ktor 相依性。
xml
<dependency>
    <groupId>io.ktor</groupId>
    <artifactId>ktor-server-thymeleaf-jvm</artifactId>
    <version>${ktor_version}</version>
</dependency>

安裝 Thymeleaf ​

若要在應用程式中安裝 Thymeleaf 外掛程式, 請將其傳遞給指定模組模組可讓您透過對路由進行分組來建構應用程式。中的 install 函式。 以下範例示範如何安裝 Thymeleaf:

  • 在 embeddedServer() 函式呼叫中。
  • 在 Application 類別上明確定義的 module() 擴充方法中。
kotlin
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.thymeleaf.*

fun main() {
    embeddedServer(Netty, port = 8080) {
        install(Thymeleaf)
        // ...
    }.start(wait = true)
}
kotlin
import io.ktor.server.application.*
import io.ktor.server.thymeleaf.*
// ...
fun Application.module() {
    install(Thymeleaf)
    // ...
}

設定 Thymeleaf ​

設定範本載入 ​

在 install 區塊內,你可以設定 ClassLoaderTemplateResolver。例如,以下程式碼片段讓 Ktor 能夠相對於目前的 classpath,在 templates 套件中查詢 *.html 範本:

kotlin
import io.ktor.server.application.*
import io.ktor.server.thymeleaf.*
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver

fun Application.module() {
    install(Thymeleaf) {
        setTemplateResolver(ClassLoaderTemplateResolver().apply {
            prefix = "templates/"
            suffix = ".html"
            characterEncoding = "utf-8"
        })
    }
}

在回應中傳送範本 ​

假設你在 resources/templates 中有一個 index.html 範本:

html
<html xmlns:th="http://www.thymeleaf.org">
    <body>
        <h1 th:text="'Hello, ' + ${user.name}"></h1>
    </body>
</html>

使用者的資料模型如下所示:

kotlin
data class User(val id: Int, val name: String)

若要將範本用於指定的路由,請透過以下方式將 ThymeleafContent 傳遞給 call.respond 方法:

kotlin
get("/index") {
    val sampleUser = User(1, "John")
    call.respond(ThymeleafContent("index", mapOf("user" to sampleUser)))
}

範例:自動重新載入 Thymeleaf 範本 ​

以下範例示範在使用開發模式時如何自動重新載入 Thymeleaf 範本。

kotlin
package com.example

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.thymeleaf.*
import org.thymeleaf.templateresolver.*

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

fun Application.module() {
    install(Thymeleaf) {
        setTemplateResolver((if (developmentMode) {
            FileTemplateResolver().apply {
                cacheManager = null
                prefix = "src/main/resources/templates/"
            }
        } else {
            ClassLoaderTemplateResolver().apply {
                prefix = "templates/"
            }
        }).apply {
            suffix = ".html"
            characterEncoding = "utf-8"
        })
    }
    routing {
        get("/index") {
            val sampleUser = User(1, "John")
            call.respond(ThymeleafContent("index", mapOf("user" to sampleUser)))
        }
    }
}

data class User(val id: Int, val name: String)

你可以在此處找到完整的範例:thymeleaf-auto-reload。