目的
htmlによって書かれたテンプレートファイルが、コントローラから呼び出されるまでの最小の手順を確認する
環境
Spring Boot 1.3.0.RELEASE
IDE STS
Java 8
手順
Spring Bootプロジェクトを作成する
pomファイルへの追記
依存性を追記する
pom.xml
<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>
テンプレートを追加
sorce/main/resources
以下に、templatesディレクトリを追加
その下に、
test/test.html
ファイルを作成
test.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>top page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
test
<p th:text="${msg}" />
</body>
</html>
コントローラを追加
com.example.webパッケージを作成し、
CustomerControllerクラスを作成する
CustomerController.java
package com.example.web;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test")
public class CustomerController {
@RequestMapping(method = RequestMethod.GET)
public String test(Model model) {
model.addAttribute("msg","サンプルメッセージ!");
return "test/test";
}
}
起動
http://localhost:8080/test
よりブラウザで表示
最終的なディレクトリ構成
mvnw
mvnw.cmd
というファイルは、プロジェクトを作成時点では存在していなかった。
起動後に生成されたよう
今後
Thymeleafでの画面作成
コントローラの内容等は個別にまた追記する。