
前質問を解決するために作成したプロジェクトでつまづいてしまいご指摘いただけると嬉しいです。
やりたいこと
データベースから値を取得して画面に表示する
詰まっているところ
画面に表示する際にthymeleafの以下エラーが発生
ERROR 5072 --- [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: An error happened during template parsing (template: "class path resource [templates/index/index.html]")] with root cause
パスがないよ、って言われているがフォルダ構成は以下で合っていると思っている。
//根っこは省略 ├─out │ └─production │ ├─classes │ │ ├─com │ │ │ └─example │ │ │ └─demo │ │ │ │ DemoApplication.class │ │ │ │ │ │ │ ├─controller │ │ │ │ IndexController.class │ │ │ │ LoginController.class │ │ │ │ │ │ │ ├─entity │ │ │ │ AccountEntity.class │ │ │ │ │ │ │ ├─repository │ │ │ │ AccountRepository.class │ │ │ │ │ │ │ └─service │ │ │ AccountService.class │ │ │ │ │ ├─generated │ │ └─META-INF │ │ com.example.demo.main.kotlin_module │ │ │ └─resources │ │ application.properties │ │ │ └─templates │ ├─index │ │ index.html │ │ │ └─login │ login.html │ └─src │ .DS_Store │ ├─main │ │ .DS_Store │ │ │ ├─java │ │ │ .DS_Store │ │ │ │ │ └─com │ │ │ .DS_Store │ │ │ │ │ └─example │ │ │ .DS_Store │ │ │ │ │ └─demo │ │ │ DemoApplication.java │ │ │ │ │ ├─controller │ │ │ IndexController.java │ │ │ LoginController.java │ │ │ │ │ ├─entity │ │ │ AccountEntity.java │ │ │ │ │ ├─repository │ │ │ AccountRepository.java │ │ │ │ │ └─service │ │ AccountService.java │ │ │ └─resources │ │ .DS_Store │ │ application.properties │ │ │ ├─db │ │ │ .DS_Store │ │ │ │ │ └─migration │ │ .DS_Store │ │ │ └─templates │ ├─index │ │ index.html ←ここでしょ? │ │ │ └─login │ login.html │ └─test
エラーでググると「Controller のreturn のあとの先頭に/をつけているから」とかいう記事が多いが先頭に/はつけていない。
ルートにマッピングしているコントローラ
java
1package com.example.demo.controller; 2 3import com.example.demo.entity.AccountEntity; 4import com.example.demo.repository.AccountRepository; 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.web.bind.annotation.RequestMapping; 9import org.springframework.web.bind.annotation.RequestMethod; 10 11import java.util.List; 12 13@Controller 14@RequestMapping("/") 15public class IndexController { 16 @Autowired 17 AccountRepository accountRepository; 18 19 @RequestMapping(value = "/", method = RequestMethod.GET) 20 public String index(Model model) { 21 List<AccountEntity> emplist=accountRepository.findAll(); 22 model.addAttribute("emplist", emplist); 23 24 model.addAttribute("message", "Hello Springboot"); 25 return "index/index"; 26 27 } 28} 29
起動してローカルホスト8080へ接続すると以下画面が出る。
ご指摘いただけると大変うれしいです
回答1件
あなたの回答
tips
プレビュー