weblog

技術的なメモ置き場。

Micronaut + Thymeleaf を試す

  • Micronaut: 1.2.2
  • Thymeleaf: 3.0.11.RELEASE

適当なディレクトリでMicronautアプリを生成。

$ mn create-app -f kotlin -i

dependenciesにmicronaut-viewsとThymeleafを追加。

build.gradle

implementation "io.micronaut:micronaut-views"
runtimeOnly "org.thymeleaf:thymeleaf:3.0.11.RELEASE"

一旦適当なテキストを返すControllerを用意する。

import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get

@Controller
class ViewController {
    @Get
    fun index() = "test"
}

アプリケーションを起動してGetしてみる。

$ curl http://localhost:8080 -i
HTTP/1.1 200 OK
Date: Mon, 23 Sep 2019 05:08:15 GMT
content-type: application/json
content-length: 4
connection: keep-alive

test

テンプレートの表示

src/main/resources にviewsディレクトリを用意し、index.html を作成する。

<!DOCTYPE html>
<html lang="ja">
<body>
test
</body>
</html>

@View

先ほど作成したControllerを修正する。
@View に作成したテンプレートファイル名を指定する。

import io.micronaut.http.HttpResponse
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.views.View

@Controller
class ViewController {
    @Get
    @View("index")
    fun index() = HttpResponse.ok<String>()
}

アプリケーションを起動してGetしてみる。

$ curl http://localhost:8080 -i
HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 23 Sep 2019 05:14:41 GMT
content-length: 44
connection: keep-alive

<!DOCTYPE html>
<html lang="ja">
<body>
test
</body>
</html>

ModelAndView

@ViewではなくModelAndViewを使っても同等のことが行える。 Controllerに以下のメソッドを追加する。

@Get("modelandview")
fun modelandview() = ModelAndView("index", "")  // 第一引数にテンプレートファイル名を指定

アプリケーションを起動してGetしてみる。

$ curl http://localhost:8080/modelandview -i
HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 23 Sep 2019 05:21:14 GMT
content-length: 44
connection: keep-alive

<!DOCTYPE html>
<html lang="ja">
<body>
test
</body>
</html>

変数の表示

以下のテンプレートファイルを用意する。

src/main/resources/views/pet.html

<!DOCTYPE html>
<html lang="ja" xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="${name}"></h1>
<h1 th:text="${age}"></h1>
</body>
</html>

POJOを使った場合

以下のPOJOを用意する。

data class Pet(
    val name: String,
    val age: Int
)

Controllerに以下のメソッドを追加する。

@Get("pojo")
@View("pet")
fun pojo() = HttpResponse.ok(Pet("taro", 10))

アプリケーションを起動してGetしてみる。

$ curl http://localhost:8080/pojo -i
HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 23 Sep 2019 05:31:51 GMT
content-length: 80
connection: keep-alive

<!DOCTYPE html>
<html lang="ja">
<body>
<h1>taro</h1>
<h1>10</h1>
</body>
</html>

Mapを使った場合

Controllerに以下のメソッドを追加する。

@Get("map")
@View("pet")
fun map() = HttpResponse.ok(mapOf("name" to "jiro", "age" to 2))

アプリケーションを起動してGetしてみる。

$ curl http://localhost:8080/map -i
HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 23 Sep 2019 05:44:01 GMT
content-length: 80
connection: keep-alive

<!DOCTYPE html>
<html lang="ja">
<body>
<h1>jiro</h1>
<h1>2</h1>
</body>
</html>

ModelAndVIewとPOJOを使った場合

Controllerに以下のメソッドを追加する。

@Get("modelandviewpojo")
fun modelAndViewAndPojo() = ModelAndView("pet", Pet("saburo", 3))

アプリケーションを起動してGetしてみる。

$ curl http://localhost:8080/modelandviewpojo -i
HTTP/1.1 200 OK
Content-Type: text/html
Date: Mon, 23 Sep 2019 05:49:24 GMT
content-length: 82
connection: keep-alive

<!DOCTYPE html>
<html lang="ja">
<body>
<h1>saburo</h1>
<h1>3</h1>
</body>
</html>