2016-12-25 11 views
0

私はhttp://www.thymeleaf.org/doc/articles/springmvcaccessdata.htmlでこのチュートリアルに従って、Thymeleafテンプレートに応答を送信する方法を学習しようとしています。しかしテンプレートの場所を見つけることができません:classpath:/ templates /(テンプレートを追加するか、Thymeleafの設定を確認してください)春のブートからThymeleafテンプレートを見つける方法

私はmessage.htmlファイルを他のソースディレクトリとsrc/main/resourcesの中に入れます<default package>

ので、構造は次のようになります。

SomeProject - その他のソース --src /メイン/リソース --- <default package> ----それは示して、なぜ私が思っていた

をmessage.html <default package>の下にありますが、<template>の下にはありませんか?それは問題だろうか?もしそうなら、私はそれをどうやって変えるのですか?私はnetbeansとmavenを使用しています。どんなアイデアですか?私は

@RequestMapping(value = "message", method = RequestMethod.GET) 
    public String messages(Model model) { 
    model.addAttribute("messages", messageRepository.findAll()); 
    return "message"; 
} 

を持って、ビュー内のコントローラで

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-devtools</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
    </dependency>    
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.h2database</groupId> 
     <artifactId>h2</artifactId> 
    </dependency> 

:これらは私のpom.xmlで、私が持っている依存関係している、ここで

<ul th:each="message : ${messages}"> 
    <li th:text="${message.id}">1</li> 
    <li><a href="#" th:text="${message.title}">Title ...</a></li> 
    <li th:text="${message.text}">Text ...</li> 
</ul> 

答えて

0

2物事: 1。 Mavenを使用している場合は、フォルダ名のカ​​スタマイズは想定していません。次に、フォルダ名はsourceではなくsrcでなければなりません。 2.フォルダの名前が変更されたら、テンプレートをsrc/resources内の 'templates'フォルダに移動します。これは正常に動作します。

+0

@Hmanshu Bhardwaj私は質問でタイプミスをしました。 netbeansでは、netbeansプロジェクト階層のOther resourcesディレクトリの下にあるsrc/main/resourcesのようなフォルダ構造が表示されます。私は質問の誤字を修正します。ご協力いただきありがとうございます。今、あなたはもう少しフィードバックを与えることができますか? – Gustav

+0

私は既に2点を与えました。テンプレートはsrc/main/resourcesの下のディレクトリにありますか? –

+0

@ Himanshu src/main/resourcesの下にtemplatesディレクトリを作成し、そこにhtmlファイルを置いて問題を解決しました。ご協力ありがとうございました。私はその質問に答えてマークします。 – Gustav

2

Springブートでは、タイメレフテンプレートエンジンの自動設定がサポートされているため、テンプレートはsrc/main/resources/templatesから自動的に取得されます。

テンプレートの場所をカスタマイズする場合、Springブートで利用可能なthymeleafプロパティ設定を使用します。

  • spring.thymeleaf.check-template = true#レンダリングする前にテンプレートが存在することを確認します。

  • spring.thymeleaf.check-template-location = true#テンプレートの場所が存在することを確認します。

  • spring.thymeleaf.enabled = true#MVC Thymeleafの表示解像度を有効にします。

  • spring.thymeleaf.prefix = classpath:/ templates /#URLを構築するときにビュー名の先頭に付加されるプレフィックス。

  • spring.thymeleaf.suffix = .html#URLを構築するときにビュー名に追加される接尾辞。

+0

どこからこの設定ファイルが見つかるはずですか?私はmavenを使用しています。依存リストから一部を変更しようとすると、そこに何か変更することはできません。いくつかのコードは隠されています。構成を変更したい依存関係を手動でダウンロードするはずですか? – Gustav

+0

クラスパスにapplication.propertiesファイルを追加し、すべてのプロパティを配置します。 src/main/resources/application.properties – Chandu

0

thymeleafテンプレートのデフォルトのディレクトリの場所は次のとおりです。

src/main/resources/templates 

他のパスは春ブートするための標準的な規則です。Thymeleafのテンプレートファイルの場合

Spring Boot thymeleaf directory structure

関連する問題