的問題是:
org.thymeleaf.exceptions.ConfigurationException: Cannot initialize: no template resolvers have been set
要Thymeleaf與Spring集成,你需要配置3種豆:
- ThymeleafViewResolver豆 - 你會用一個模板引擎
- SpringTemplateEngine被設置豆 - 你將被設置一個模板解析器
- TemplateResolver Bean
在你templateEngine
豆你沒有設置任何模板解析器,所以你可能會改變你templateEngine()
方法如下:
@Bean
public SpringTemplateEngine templateEngine(TemplateResolver templateResolver){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
templateEngine.addDialect(new LayoutDialect());
return templateEngine;
}
春天將爲您提供一個templateResolver
豆的SpringTemplateEngine
。
BTW如果定義spring-boot-starter-thymeleaf
的依賴,它將提供thymeleaf-layout-dialect
與便捷的版本存在依賴關係,那麼Spring將使用ThymeleafAutoConfiguration.java
- Spring Boot 1.5.x配置默認豆所需的三種豆。
例如:
LayoutDialect
bean是定義在這裏ThymeleafWebLayoutConfiguration.ThymeleafWebLayoutConfiguration():
@Configuration
@ConditionalOnClass(name = "nz.net.ultraq.thymeleaf.LayoutDialect")
protected static class ThymeleafWebLayoutConfiguration {
@Bean
@ConditionalOnMissingBean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}
}
SpringTemplateEngine
豆是從這裏ThymeleafWebLayoutConfiguration.ThymeleafDefaultConfiguration()定義的模板解析器和方言:
@Configuration
@ConditionalOnMissingBean(SpringTemplateEngine.class)
protected static class ThymeleafDefaultConfiguration {
private final Collection<ITemplateResolver> templateResolvers;
private final Collection<IDialect> dialects;
public ThymeleafDefaultConfiguration(
Collection<ITemplateResolver> templateResolvers,
ObjectProvider<Collection<IDialect>> dialectsProvider) {
this.templateResolvers = templateResolvers;
this.dialects = dialectsProvider.getIfAvailable();
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
for (ITemplateResolver templateResolver : this.templateResolvers) {
engine.addTemplateResolver(templateResolver);
}
if (!CollectionUtils.isEmpty(this.dialects)) {
for (IDialect dialect : this.dialects) {
engine.addDialect(dialect);
}
}
return engine;
}
}
最後一個thymeleafViewResolver
bean在這裏定義爲AbstractThymeleafViewResolverConfiguration.thymeleafViewResolver():
@Bean
@ConditionalOnMissingBean(name = "thymeleafViewResolver")
@ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true)
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
configureTemplateEngine(resolver, this.templateEngine);
resolver.setCharacterEncoding(this.properties.getEncoding().name());
resolver.setContentType(appendCharset(this.properties.getContentType(),
resolver.getCharacterEncoding()));
resolver.setExcludedViewNames(this.properties.getExcludedViewNames());
resolver.setViewNames(this.properties.getViewNames());
// This resolver acts as a fallback resolver (e.g. like a
// InternalResourceViewResolver) so it needs to have low precedence
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
resolver.setCache(this.properties.isCache());
return resolver;
}
這是由ThymeleafAutoConfiguration.Thymeleaf2ViewResolverConfiguration
擴展:
@Bean
@ConditionalOnMissingBean(name = "thymeleafViewResolver")
@ConditionalOnProperty(name = "spring.thymeleaf.enabled", matchIfMissing = true)
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
configureTemplateEngine(resolver, this.templateEngine);
resolver.setCharacterEncoding(this.properties.getEncoding().name());
resolver.setContentType(appendCharset(this.properties.getContentType(),
resolver.getCharacterEncoding()));
resolver.setExcludedViewNames(this.properties.getExcludedViewNames());
resolver.setViewNames(this.properties.getViewNames());
// This resolver acts as a fallback resolver (e.g. like a
// InternalResourceViewResolver) so it needs to have low precedence
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
resolver.setCache(this.properties.isCache());
return resolver;
}
希望它現在很清楚。
你可以檢查是否有幫助:http://stackoverflow.com/questions/43197416/spring-boot-thymeleaf-error-java-lang-classnotfoundexception-org-thymeleaf-do/43203400#43203400 –