7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Spring Boot + ThymeleafでWEBアプリを動かす

Posted at

Spring MVCを修正する機会が多かったのですがBootをあまり利用して来なかったので復習も兼ねて動かします。

構成

Mac OS X 10.12
Java 1.8.0_92
Spring Tool Suite 3.8.4
Spring Boot 1.5.3
thymeleaf 2.1.5

導入

1.JavaをOracleの公式からDLしてインストールします。

2.Spring Tool Suite(以降「STS」)を公式からDLしてインストールします。

3.プロジェクト作成
STSで以下のように作成
1.png

2.png

3.png

4.コントローラとテンプレートの作成
以下、2つのファイルを作成します
4.png

Java

SampleController.java
package com.example;

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("/sample")
public class SampleController {

	@RequestMapping(method = RequestMethod.GET)
	public String test(Model model) {
		model.addAttribute("name", "草苅API");
		model.addAttribute("get", "GET /sample");
		model.addAttribute("post", "POST /sample");
		return "sample/index";
	}

}

HTML

index.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>
<h2 th:text="${name}"></h2>
  <p th:text="${get}" /> 
  <p th:text="${post}" /> 
</body>
</html>

5.WEBアプリを動かす
5.png

想定通りに動きました
6.png

7
9
1

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
7
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?