SpringBoot2.7とJDK14でhello worldを表示する方法

環境
SpringBoot2.7
Spring Tool Suite 4.8.0.RELEASE
JavaSE 14
thymeleaf3

操作方法
1.コントローラーのクラスTestController.java
以下の内容を編集します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class TestController { // Controllerのアノテーション
@GetMapping("/test123") // GetMappingのアノテーション
public String funA(Model model) {
model.addAttribute("city", "東京都");
model.addAttribute("username", "山田太郎");
return "test";
}
}
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @Controller public class TestController { // Controllerのアノテーション @GetMapping("/test123") // GetMappingのアノテーション public String funA(Model model) { model.addAttribute("city", "東京都"); model.addAttribute("username", "山田太郎"); return "test"; } }
package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController { // Controllerのアノテーション

      @GetMapping("/test123") // GetMappingのアノテーション
      public String funA(Model model) {
            model.addAttribute("city", "東京都");
            model.addAttribute("username", "山田太郎");
            return "test";
      }

}

2.htmlファイルの作成
src/main/resourcesのtemplates/test.html
以下の内容を編集します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>springからのパラメータの値が表示</title>
</head>
<body>
都市名: <p th:text="${city}"></p>
名前: <p th:text="${username}"></p>
</body>
</html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>springからのパラメータの値が表示</title> </head> <body> 都市名: <p th:text="${city}"></p> 名前: <p th:text="${username}"></p> </body> </html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <meta charset="utf-8" />
    <title>springからのパラメータの値が表示</title>
  </head>
  <body>
    都市名: <p th:text="${city}"></p>
    名前: <p th:text="${username}"></p>
  </body>
</html>

3.application.propertiesの作成
src/main/resources/application.properties
#ポート番号
server.port=8766

4.pom.xmlの修正
以下の内容を編集します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency>
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
      <scope>runtime</scope>
      <optional>true</optional>
</dependency>

5.動作確認
http://127.0.0.1:8766/test123にアクセスします
「東京都」と「山田太郎」が表示されます。

Spring Boot

Posted by arkgame