Spring Boot添加thymeleaf-layout-dialect

时间:2017-04-14 16:20:02

标签: spring-mvc spring-boot thymeleaf

我正在使用春季靴子(v1.5.3.BUILD-SNAPSHOT) 我是春季靴子的新手。

使用gradle

请注意,正常的百里香方言工作正常(th:...) 弹簧引导起动thymeleaf \ 1.5.3.BUILD-SNAPSHOT

我想添加百里香布置方言 我添加了依赖

compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect')

documentation表示通过执行以下操作来添加方言

TemplateEngine templateEngine = new TemplateEngine();  // Or 
SpringTemplateEngine for Spring config
templateEngine.addDialect(new LayoutDialect());

所以我添加了一个配置类

@Configuration
public class MyConfiguration {


@Bean
public SpringTemplateEngine templateEngine(){
    SpringTemplateEngine templateEngine  = new SpringTemplateEngine();
    templateEngine.addDialect(new LayoutDialect());
    return templateEngine;
}
}

但是当我尝试运行该应用时,我收到以下错误

org.thymeleaf.exceptions.ConfigurationException: Cannot initialize: no template resolvers have been set
at org.thymeleaf.Configuration.initialize(Configuration.java:203) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.TemplateEngine.initialize(TemplateEngine.java:827) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:203) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282) ~[spring-webmvc-4.3.8.BUILD-SNAPSHOT.jar:4.3.8.BUILD-SNAPSHOT]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) ~[spring-webmvc-4.3.8.BUILD-SNAPSHOT.jar:4.3.8.BUILD-SNAPSHOT]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) ~[spring-webmvc-4.3.8.BUILD-SNAPSHOT.jar:4.3.8.BUILD-SNAPSHOT]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.8.BUILD-SNAPSHOT.jar:4.3.8.BUILD-SNAPSHOT]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.8.BUILD-SNAPSHOT.jar:4.3.8.BUILD-SNAPSHOT]

有人能告诉我如何正确添加百里香 - 布局 - 方言吗?

1 个答案:

答案 0 :(得分:0)

问题是:

org.thymeleaf.exceptions.ConfigurationException: Cannot initialize: no template resolvers have been set

要将Thymeleaf与Spring集成,您需要配置3个bean:

  • ThymeleafViewResolver Bean - 您将使用模板引擎
  • 进行设置
  • SpringTemplateEngine Bean - 您将使用模板解析器设置它
  • TemplateResolver Bean

在您的templateEngine bean中,您没有设置任何模板解析程序,因此您可以更改templateEngine()方法,如下所示:

@Bean
public SpringTemplateEngine templateEngine(TemplateResolver templateResolver){
    SpringTemplateEngine templateEngine  = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);       
    templateEngine.addDialect(new LayoutDialect());
    return templateEngine;
}

Spring将为您提供templateResolver的{​​{1}} bean。

BTW 如果将SpringTemplateEngine定义为依赖项,它将提供spring-boot-starter-thymeleaf作为方便版本的依赖项,然后Spring将使用ThymeleafAutoConfiguration.java - Spring Boot 1.5.x来配置三个必需bean的默认bean。

例如:

thymeleaf-layout-dialect bean在这里定义ThymeleafWebLayoutConfiguration.ThymeleafWebLayoutConfiguration()

LayoutDialect

@Configuration @ConditionalOnClass(name = "nz.net.ultraq.thymeleaf.LayoutDialect") protected static class ThymeleafWebLayoutConfiguration { @Bean @ConditionalOnMissingBean public LayoutDialect layoutDialect() { return new LayoutDialect(); } } bean是使用模板解析器和方言ThymeleafWebLayoutConfiguration.ThymeleafDefaultConfiguration()定义的:

SpringTemplateEngine

最后在AbstractThymeleafViewResolverConfiguration.thymeleafViewResolver()定义@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; } } bean:

thymeleafViewResolver

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;
}

希望现在很清楚。