2017-02-03 11 views
0

ThymeleafでSpringブートベースのアプリケーションを作成しようとしています。私は開始点としてPetClinicサンプルを使用しています。 私のアプリケーションは、いくつかのテンプレートを見つけることができません。SpringブートとThymeleaf:HTMLテンプレートが見つかりません

私のプロジェクトは、標準的な方法で設定されています

/src 
/main 
    /java 
    /com.example 
    MyWebApplication.java 
    HomeController.java 
    /resources 
    /static 
    /css 
    /templates 
    /fragments 
    layout.html 
    home.html 
    application.properties 

のpom.xml

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.1.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
</parent> 

<properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <java.version>1.8</java.version> 
</properties> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-devtools</artifactId> 
     <optional>true</optional> 
    </dependency>     
</dependencies> 

MyWebApplication.java

@SpringBootApplication 
public class MyWebApplication { 
    public static void main(String[] args) { 
     SpringApplication.run(MyWebApplication.class, args); 
    } 
} 

が空0 HomeController.java

home.html

<!DOCTYPE html> 

<html xmlns:th="http://www.thymeleaf.org" 
     xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" 
    th:replace="~{fragments/layout :: layout (~{::body},'home')}"> 
<body> 
    <h2 th:text="#{welcome}">Welcome</h2> 
     <div class="row"> 
     </div> 
</body> 

</html> 

layout.html

<!DOCTYPE html> 

<!-- 
    The main layout fragment that defines the overall layout 
    Sets up the navigation bar 
    The page contents are replaced in the main div 
--> 
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout" 
     th:fragment="layout (template, menu)"> 

<head> 

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 

    <!-- <link rel="shortcut icon" type="image/x-icon" th:href="@{/images/favicon.png}"> --> 

    <title>Web Interface</title> 

    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" /> 

</head> 

<body> 

    <!-- Navigation Bar --> 
    <nav class="navbar navbar-default" role="navigation"> 
    <div class="container"> 
     <div class="navbar-header"> 
     <a class="navbar-brand glyphicon glyphicon-home" th:href="@{/}"></a> 

     <!-- Collapse the menu bar on small windows --> 
     <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#main-navbar"> 
        <span class="sr-only">Toggle navigation</span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span> 
        <span class="icon-bar"></span>   
     </button>  
     </div> 

     <!-- The Navbar items --> 
     <div class="navbar-collapse collapse" id="main-navbar"> 
     <ul class="nav navbar-nav navbar-right"> 

      <!-- Define a fragment for each menu item --> 
      <li th:fragment="menuItem (path, active, title, glyph, text)" th:class="${active==menu ? 'active' : ''}"> 
      <a th:href="@{__${path}__}" th:title=${title}> 
       <span th:class="'glyphicon glyphicon-'+${glyph}" class="glyphicon glyphicon-home" aria-hidden="true"></span> 
       <span th:text="${text}">Template</span> 
      </a> 
      </li> 

      <!-- Replace with the fragment --> 
      <li th:replace="::menuItem ('/', 'home', 'home page', 'home', 'Home')"> 
      <span class="glyphicon glyphicon-home" aria-hidden="true"></span> 
      <span> Home</span> 
      </li>     
     </ul> 
     </div> 
    </div> 
    </nav> 

    <!-- Main body --> 
    <div class="container-fluid"> 
    <div class="container xd-container"> 
     <div th:replace="${template}"></div> 
    </div> 

    </div> 

    <script th:src="@{/js/bootstrap.min.js}" type="text/javascript"></script> 

</body> 

</html> 

application.properties。私は「目:交換する」削除した場合

Error resolving template "~{fragments/layout", template might not exist or might not be accessible by any of the configured Template Resolvers (home:5)

:私はhttp://localhost:8080に移動すると

が、私はエラーを取得するhome.htmlから属性を、それがウェルカムメッセージが表示されます。

類似の質問(例:this one)を参照すると、他の人がThymeleafViewResolver beanを作成することがわかります。

質問:

  1. は、なぜそれが私のテンプレートを見つけることができませんか?
  2. テンプレートリゾルバを作成する必要はありますか?もしそうなら、どのようにペットクリニックのサンプルはありませんか?テンプレートリゾルバはどこから来たのですか?

私が見る限り、それはペットクリニックのサンプルと同じ方法で設定されているので、違いは何ですか?

答えて

1

スプリングブートは自動的にあなたのためにいくつかの内部作業を行いますが、自動化作業を行うには適切に設定する必要があります。これはあなたの質問2の答えです。springは設定が正しい場合、自動的にviewリゾルバを提供します。さて、質問1に移動しましょう。

あなたのpom.xmlとpetclinicを比較すると、いくつかの重要な違いがあります。

最初に、スプリング公式ドキュメントhttp://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-build-systems.html#using-boot-mavenによると、Mavenユーザーはspring-boot-starter-parentプロジェクトから継承して、賢明なデフォルトを取得できます。親プロジェクトはいくつかの機能を提供します。 spring-boot-starter-parentから継承するようにプロジェクトを設定するには、単にpom.xmlに親を設定します。

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.1.RELEASE</version> 
</parent> 

この設定では、自分のプロジェクトのプロパティを上書きすることによって個々の依存関係を無効にすることもできます。例えば、petclinic pom.xmlでは、彼らはあなたの中に欠けているthymeleafバージョンを指定しました。今

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> 

あなたは春・ブート・スターター-thymeleafからthymeleafレイアウト-方言を除外する必要がそれらの間の互換性を持つ適切に設定ThymeleafとThymeleafレイアウト方言バージョンに、3.0.2.RELEASEにthymeleafバージョンを設定するbczバージョン。

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
     <exclusions> 
     <exclusion> 
      <groupId>nz.net.ultraq.thymeleaf</groupId> 
      <artifactId>thymeleaf-layout-dialect</artifactId> 
     </exclusion> 
     </exclusions> 
    </dependency> 
+0

thymeleaf-layout-dialectアーチファクトを除外する必要があるのはなぜですか? – devdanke

+0

トレーニングホイールの "-starter"アーティファクトなしでthymeleafとspring-bootを使用する方法を示す例はありますか? "-starter"アーティファクトは、デモ/ラーニングの目的であるようです。残念ながら、彼らはthymeleafとspring-bootが実際にどのように動作するかを隠しています。 – devdanke

関連する問題