2014-05-08 71 views

回答

30

注:试图在此之前,请注意后续版本春天启动的包括一些常见的方言的开箱即用。请参阅@Robert Hunt的answer。否则:

有一个示例here添加了Dialect bean,Spring Boot将自动检测和使用(请参阅LayoutDialect代码和ThymeleafDefaultConfiguration类的方言成员)。在你的情况下,加在你@Configuration类之一的以下内容:

@Bean 
public ConditionalCommentsDialect conditionalCommentDialect() { 
    return new ConditionalCommentsDialect(); 
} 

春天开机,在ThymeleafAutoConfiguration类,会自动添加实现IDialect接口的bean。

+14

你为什么在第二个人回答你自己的问题? – xdhmoore

+0

您的评论花了我的时间超过它应该...无论如何,感谢分享! –

11

随着春天启动的1.2.1版一些额外的方言已经被添加到ThymeleafAutoConfiguration类将被自动检测到,如果他们在类路径包括:

只需在类路径上放置JAR就足以让Spring Boot注册它们。

注意:如果您使用的是spring-boot-starter-thymeleaf,那么您会发现默认情况下已包含LayoutDialect

+0

我有点困惑,请你帮忙。 spring-boot-starter-parent的最新版本是1.5.2。使用这种配置,百里香的版本可以解决2.某些问题。鉴于根据https://ultraq.github.io/thymeleaf-layout-dialect/Installation.html,布局方言需要布局方言的百里香叶3.0 –

+1

@djaqeel版本2.0.0,并且超出支持时效3。该网站有点含糊不清,因为它说“至少需要Java 7和Thymeleaf 3.0”,但在页面底部的“迁移到2.0”指南中,它指出“Thymeleaf 3与Thymeleaf 2模板基本向后兼容,所以布局方言也走出了向后兼容的道路。“所以它可能只是值得一试。 Spring Boot 1.5.2的“thymeleaf-layout-dialect”版本设置为版本1.4.0的依赖管理版本,因此该版本可以正常工作。 –

+0

谢谢。是否有任何链接到布局方言1.4.0教程。因为https://ultraq.github.io/thymeleaf-layout-dialect/Examples.html#layouts似乎不适合我。布局文件没有被应用。 –

5

我其实认为ThymeleafAutoConfiguration有缺陷。如果代码位于类路径中,我会看到代码应该在哪里找到并添加SpringSecurityDialect到Config中,但在调试中,这根本没有发生(只有LayoutDialect被定向并添加到配置中)。我有我的类路径中SpringSecurityDialect类/罐,但低于豆从未被SpringBoot自动配置(ThymeleafAutoConfig.java,行97)

@Configuration 
@ConditionalOnClass({SpringSecurityDialect.class}) 
protected static class ThymeleafSecurityDialectConfiguration { 
    protected ThymeleafSecurityDialectConfiguration() { 
    } 

    @Bean 
    @ConditionalOnMissingBean 
    public SpringSecurityDialect securityDialect() { 
     return new SpringSecurityDialect(); 
    } 
} 

到底添加到配置,我不得不实际添加一个bean到我的自定义Java配置来获得SpringSecurityDialog识别:

@Bean 
public SpringSecurityDialect securityDialect() { 
    return new SpringSecurityDialect(); 
} 

这工作第一次。你可能有一些测试来验证这是一个已知的问题吗?我包括我的pom.xml

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

    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.thymeleaf.extras</groupId> 
     <artifactId>thymeleaf-extras-springsecurity4</artifactId> 
     <version>2.1.2.RELEASE</version> 
    </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-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
+0

它应该工作而不必手动指定bean。我没有使用Spring Boot 1.2.5进行测试,但支持是在1.2.1中添加的,所以它应该可以工作。我没有签出这个样本:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-method-security并添加了“thymeleaf-extras -springsecurity4“依赖关系,并自动注册。您可能想要使用--debug标志运行应用程序,或添加spring-boot-actuator并读取自动配置报告。 –

+0

谢谢,我对1.3.3.RELEASE和defintion有同样的问题修复它。它也似乎在1.4中被修复。 –

+1

即使在我添加了所有建议之后,我仍然在线程“main”java.lang.NoClassDefFoundError:org/thymeleaf/dialect/IExpressionEnhancingDialect中收到异常 – yglodt