SpringBootの環境構築
EclipseにSpringBootの環境を作成したときのメモ
構築環境
- Pleiades All in One
- Eclipse 2022
Spring スターター・プロジェクト(Spring Initializr)の作成
1.ファイル > 新規 > その他

2.Spring Boot >Spring スターター・プロジェクト(Spring Initializr) > 「次へ」をクリック

3.Spring Boot >「次へ」をクリック

4.「完了」をクリック

- ※SpringBootfバージョンに、3以上を指定すると動かないので注意。要調査
- ※Thymeleaf、Lombok、SpringWebがチェックされていること
html(thymeleaf)の作成
1.SpringSampleProject > src/main/resources > templates(右クリック) > 新規 > ファイル

- ※注意 「HTMLファイル」を選択するとwebappの配下にファイルが作られてしまう
2.ファイル名に「ThymeleafSample.html」と入力して「完了」をクリック

3.ThymeleafSample.htmlが作成される。

4.ThymeleafSample.htmlの内容を下記の通りに編集して保存する
- 1 :
2 :
3 :
4 :
5 :
6 :
7 :
8 :
9 :
10 : -
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title<Insert title here</title> </head> <body> <div th:text="${SampleString}"></div> </body> </html>
コントローラクラスの作成
1.SpringSampleProject > src/main/java > com.example.demo(右クリック) > 新規 > パッケージ

2.名前に「com.example.demo.controller」を入力して「完了」をクリック

3.パッケージ「com.example.demo.controller」が作成される

4.SpringSampleProject > src/main/java > com.example.demo.controller(右クリック) > 新規 > クラス

5.名前に「ControllerSample」を入力して「完了」をクリック

6.クラス「ControllerSample.java」が作成される。

7.ControllerSampleの内容を下記の通り編集して保存する。
- 1 :
2 :
3 :
4 :
5 :
6 :
7 :
8 :
9 :
10 :
11 :
12 :
13 :
14 :
15 :
16 :
17 :
18 :
19 :
20 :
21 :
22 : -
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @RequestMapping("/") @Controller public class ControllerSample { @GetMapping("Sample") public String sample(Model model) { String str = "画面出力文字列サンプル"; model.addAttribute("SampleString", str); return "ThymeleafSample"; } }
SpringBootアプリケーションの起動
1.SpringSampleProject(右クリック) > 実行 > SpringBootアプリケーション

2.コンソールにログが出力されて、SpringBootが起動される。

3.ブラウザから下記URLにアクセス
http://localhost:8080/Sample
