LoginSignup
1
0

More than 1 year has passed since last update.

Spring Boot + Thymeleaf の template resolving でハマった

Last updated at Posted at 2022-08-27

環境

  • IntelliJ
  • Spring Boot 2.7.3
  • Thymeleaf
  • Kotlin 1.6.21

概要

以下の手順で Spring Boot + Thymeleaf を利用した画面有りの Web アプリケーション作成を試みた.

  1. Spring Initializr で gradle プロジェクトを作成
  2. ローカル環境で html や kt など諸々のファイルを作成
  3. IntelliJ でアプリケーションを実行し,Web ブラウザで localhost/xxx にアクセスすると問題なく画面が表示される
  4. gradlew build を実行して出力した jar ファイルを実行して,Web ブラウザでアクセスすると以下のエラーが発生
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [/xxx/yyy], template might not exist or might not be accessible by any of the configured Template Resolvers

つまりテンプレートが存在しないか,アクセス権限がないらしい.

でも Intellij で実行した場合には問題なく画面が表示されていた.何故?

結論

fun getTop(
  modelAndView: ModelAndView
): ModelAndView =
  modelAndView.apply {
    viewName = "/xxx/yyy"
  }

誤: viewName = "/xxx/yyy"
正: viewName = "xxx/yyy"

Intellij で実行した場合やコマンド gradlew bootrun で実行した場合には,viewName の先頭に / が付いていてもよしなに解決してくれるらしい (理由調査中).

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0