2017-04-30 85 views
1

Thymeleafテンプレートファイルをデフォルトのsrc/main/resources/templatesに配置すると問題なく動作します。ディレクトリの名前をmytemplatesと変更したいときそれは動作しません。SpringブートでJava設定のThymeleafテンプレートディレクトリを変更できない

私はは、テンプレートの場所を見つけることができません受信:アプリケーションの起動を とき/テンプレート/(いくつかのテンプレートを追加したり、Thymeleafの設定を確認してください)警告:クラスパスを。

私はホームページをポイントすると、私はorg.thymeleaf.exceptions.TemplateInputException取得:エラー解消テンプレート「インデックス」を、テンプレートが存在しないか、または構成されたテンプレートのリゾルバエラーのいずれかによってアクセス可能ではないかもしれません。私が間違っているのは何

package com.zetcode.conf; 

import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.Description; 
import org.springframework.web.servlet.ViewResolver; 
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 
import org.thymeleaf.spring4.SpringTemplateEngine; 
import org.thymeleaf.spring4.view.ThymeleafViewResolver; 
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; 

@Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter { 

    @Bean 
    @Description("Thymeleaf template resolver serving HTML 5") 
    public ClassLoaderTemplateResolver templateResolver() { 

     ClassLoaderTemplateResolver tres = new ClassLoaderTemplateResolver(); 

     tres.setPrefix("classpath:/mytemplates/"); 
     tres.setSuffix(".html");   
     tres.setCacheable(false); 
     tres.setTemplateMode("HTML5"); 
     tres.setCharacterEncoding("UTF-8"); 

     return tres; 
    } 

    @Bean 
    @Description("Thymeleaf template engine with Spring integration") 
    public SpringTemplateEngine templateEngine() { 

     SpringTemplateEngine templateEngine = new SpringTemplateEngine(); 
     templateEngine.setTemplateResolver(templateResolver()); 

     return templateEngine; 
    } 

    @Bean 
    @Description("Thymeleaf view resolver") 
    public ViewResolver viewResolver() { 

     ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); 

     viewResolver.setTemplateEngine(templateEngine()); 
     viewResolver.setCharacterEncoding("UTF-8"); 
     viewResolver.setCache(false); 
     viewResolver.setOrder(1); 

     return viewResolver; 
    }  

    @Override 
    public void addViewControllers(ViewControllerRegistry registry) { 
     registry.addViewController("/").setViewName("index"); 
    } 
} 

私は、次のJavaの設定を使用できますか?

答えて

2

は、以下のことを試してみてください。

第一:今、あなたの実装をカスタマイズapplication.propertiesファイルに

spring.thymeleaf.templateResolverOrder=1 

の設定の下に定義します。

@Bean 
public ClassLoaderTemplateResolver yourTemplateResolver() { 
     ClassLoaderTemplateResolver yourTemplateResolver = new ClassLoaderTemplateResolver(); 
     yourTemplateResolver.setPrefix("yourTemplates/"); 
     yourTemplateResolver.setSuffix(".html"); 
     yourTemplateResolver.setTemplateMode(TemplateMode.HTML); 
     yourTemplateResolver.setCharacterEncoding("UTF-8"); 
     yourTemplateResolver.setOrder(0); // this is iportant. This way spring 
              //boot will listen to both places 0 
              //and 1 
     emailTemplateResolver.setCheckExistence(true); 

     return yourTemplateResolver; 
    } 

出典:デフォルトではSeveral template locations for Thymeleaf in Spring Boot

+0

なぜそれがうまくいかないのかというヒントがありました。しかし、私はそれを働かせることができませんでした。スプリングブートは同じエラーメッセージを表示し続けます。 (setCheckExistence()メソッドはありません) –

+0

最後に、それを動作させました。 –

2

は、thymeleafテンプレートがのsrc /メイン/リソース/テンプレートフォルダから読み込まれます。 Chanduは、カスタムテンプレートのロケーション名の解決策を示しています。

まず、クラスパスにapplication.propertiesファイルを追加し、次のすべてのプロパティーをsrc/main/resources/application.propertiesに入れます。

spring.thymeleaf.check-template=true # Check that the template exists before rendering it. 
spring.thymeleaf.check-template-location=true # Check that the templates location exists. 
spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution. 
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL. 
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL. 

リソースリンク:

ClassLoaderTemplateResolver tres = new ClassLoaderTemplateResolver(); 
tres.setPrefix("mytemplates/"); 

ClassLoaderTemplateResolverを使用するときに何classpath: preffixがあってはならない。ここでhttps://stackoverflow.com/a/41319170/2293534

1

は、私が考え出したものです。

テンプレートディレクトリは空であってはなりません。 Javaの設定がプロパティの設定よりも優先されます。 (注文を使用してもこれには影響しないようです。)

関連する問題