Spring 2.7.0 とGradle 7.4.1環境でhello worldを出力するサンプル
環境
Spring Boot 2.7.0
JavaSE 17
Eclipse 2022-06 M2 (4.24.0 M2)
Gradle 7.4.1
Thymeleaf3
Tomcat 9.0.63
Spring Boot 2.7.0
JavaSE 17
Eclipse 2022-06 M2 (4.24.0 M2)
Gradle 7.4.1
Thymeleaf3
Tomcat 9.0.63
Spring Boot 2.7.0 JavaSE 17 Eclipse 2022-06 M2 (4.24.0 M2) Gradle 7.4.1 Thymeleaf3 Tomcat 9.0.63
操作方法
1.コントローラー側クラスの作成(UserController.java)
以下の内容を編集します。
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
//アノテーションController
@Controller
public class UserController {
//GetMappingアノテーション
@GetMapping("/cft")
public String funA(Model model) {
model.addAttribute("city", "東京");
return "show";
}
}
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
//アノテーションController
@Controller
public class UserController {
//GetMappingアノテーション
@GetMapping("/cft")
public String funA(Model model) {
model.addAttribute("city", "東京");
return "show";
}
}
package com.example.demo; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; //アノテーションController @Controller public class UserController { //GetMappingアノテーション @GetMapping("/cft") public String funA(Model model) { model.addAttribute("city", "東京"); return "show"; } }
2.Thymeleaf3用htmlの作成(show.html)
場所 src/main/resourcesのtemplates\show.html
以下の内容を編集します。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>hello</title>
</head>
<body>
<div th:text="${city}"> </div>
</body>
</html>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>hello</title>
</head>
<body>
<div th:text="${city}"> </div>
</body>
</html>
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="utf-8" /> <title>hello</title> </head> <body> <div th:text="${city}"> </div> </body> </html>
3.application.propertiesにポートを変更します
場所 src/main/resources/application.properties
#ポート番号
server.port=8768
#ポート番号
server.port=8768
#ポート番号 server.port=8768
4.build.gradleの確認
plugins {
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
plugins {
id 'org.springframework.boot' version '2.7.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
plugins { id 'org.springframework.boot' version '2.7.0' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '17' configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' } tasks.named('test') { useJUnitPlatform() }
5.動作確認
プロジェクトを右クリックし「実行(R)」->「Spring Bootアプリケーション」を実行します。
http://127.0.0.1:8768/cftにアクセスします。
画面に「東京」が表示されます。