LoginSignup
28
29

More than 5 years have passed since last update.

Spring Boot + Thymeleaf による画面表示を行うまで(プロジェクトの新規作成→画面表示まで)

Last updated at Posted at 2015-11-24

目的

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";
    }

}

起動

スクリーンショット 2015-11-24 23.37.33.png

スクリーンショット 2015-11-24 23.35.56.png

http://localhost:8080/test
よりブラウザで表示

以下のように表示されたことを確認
スクリーンショット 2015-11-24 23.36.50.png

最終的なディレクトリ構成

スクリーンショット 2015-11-24 23.43.49.png

mvnw
mvnw.cmd
というファイルは、プロジェクトを作成時点では存在していなかった。
起動後に生成されたよう

今後

Thymeleafでの画面作成
コントローラの内容等は個別にまた追記する。

28
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
29