SpringBootのサポートするテンプレートエンジン
– Groovy, Thymeleaf, FreeMaker, Mustache
Tymeleaf利点
– 独自タグなし、ブラウザ表示
src/main/resources/templates/index.html
<!Doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>SpringBoot</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta charset="UTF-8">
<style type="text/css">
form {
border-style: solid;
border-color: black;
border-width: 1px;
margin: 5px;
padding: 10px;
}
</style>
</head>
<body>
<h1 th:text="'これはTypeleafですか?'">html-見出し1</h1>
Spring bootで推奨されています。
</body>
</html>
Run As -> SpringBoot app
http://localhost:8080/

WhatTimeIsItController.java
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("view/what-time-is-it")
@Controller
public class WhatTimeIsItController {
}
– @RequestMappingを設定
– Controllerで動的な値を生成
– 設定した動的な値をThymeleafのviewで参照
what time is it
package com.example.demo;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RequestMapping("view/what-time-is-it")
@Controller
public class WhatTimeIsItController {
@GetMapping()
public String view(Model model) {
String now = LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME);
model.addAttribute("datetime", now);
return "wtii"; // viewを返す
}
}
Run As -> SpringBoot app

やべ、だいぶわかってきた。
ちょっと理解すると、アプリ作りたい衝動が抑えられなくなってくる
MVC:ControllerがMVC:Viewにデータを渡すのにui:Modelを使用する
model.addAttribute(“”,)で値を設定している
SpEL(Spring Expression Language)とうい方式を使っている
${変数式}、*{選択変数式}、#{メッセージ式}、@{リンク式} などがある