Spring Boot + thymelaefで最小構成で動かそうとしていますが、うまくいかないので質問を投稿いたします。
前提
- OS: Windows 10
- IDE: Intellij IDEA
- Java11
- `>java --version
openjdk 11.0.2 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
`
- Project SDKも同様にJDK11
ファイル構成
src\main\java\com\hoge\practice\spring\demo\controller\SampleController.java
src\main\java\com\hoge\practice\spring\demo\DemoApplication.java
src\main\resources\templates\test.html
src\main\resources\application.yml
build.gradle
ソースファイル内容
それぞれのファイルの内容は以下です。
SampleController.java
java
1package com.hoge.practice.spring.demo.controller; 2 3import org.springframework.stereotype.Controller; 4import org.springframework.ui.Model; 5import org.springframework.web.bind.annotation.GetMapping; 6import org.springframework.web.bind.annotation.RequestMapping; 7 8@Controller 9@RequestMapping(value = "/sample") 10public class SampleController { 11 12 @GetMapping("/test") 13 public String test(Model model){ 14 model.addAttribute("title", "タイトル"); 15 return "test"; 16 } 17}
DemoApplication.java
java
1package com.hoge.practice.spring.demo; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6@SpringBootApplication 7public class DemoApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(DemoApplication.class, args); 11 } 12} 13
test.html
html
1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="UTF-8" /> 5 <title th:text="${title}">Insert title here</title> 6</head> 7<body> 8<h1 th:text="${title}">title</h1> 9<p> this is a sample page </p> 10</body> 11</html>
application.ymlは空
build.gradle
plugins { id 'org.springframework.boot' version '2.2.2.RELEASE' id 'io.spring.dependency-management' version '1.0.8.RELEASE' id 'java' } group = 'com.hoge.practice.spring.demo' version = '0.0.1-SNAPSHOT' sourceCompatibility = '11' configurations { developmentOnly runtimeClasspath { extendsFrom developmentOnly } compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-validation' compileOnly 'org.projectlombok:lombok' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor' annotationProcessor 'org.projectlombok:lombok' testImplementation('org.springframework.boot:spring-boot-starter-test') { exclude group: 'org.junit.vintage', module: 'junit-vintage-engine' } } test { useJUnitPlatform() }
エラー内容
この状態で、 http://localhost:8080/sample/test
にアクセスすると、Whitelabel Error Pageが表示されます。
コンソールのエラーの抜粋は以下です。
2020-01-09 22:37:54.229 ERROR 12060 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "test": Error resolving template [test], template might not exist or might not be accessible by any of the configured Template Resolvers org.thymeleaf.exceptions.TemplateInputException: Error resolving template [test], template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE] (中略) 2020-01-09 22:37:54.233 ERROR 12060 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [test], template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause org.thymeleaf.exceptions.TemplateInputException: Error resolving template [test], template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
また、気になる情報として、起動時のログに以下の警告が出ていました。
2020-01-09 22:37:50.172 WARN 12060 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
また、ページを開いた際、コントローラーの処理は通っています。
実現したいこと
http://localhost:8080/sample/test
にアクセスすると、test.htmlの内容が表示されること
何時間か調べましたが、Controllerの戻り値は'test.html'ではなくて'test'にしろみたいな記事しかなくて困っています。
よろしくお願いいたします。
あなたの回答
tips
プレビュー