【发布时间】:2014-06-25 05:51:06
【问题描述】:
我正在使用 Spring Boot,我想添加 IE conditional comments Thymeleaf dialect。
我已将它包含在我的 maven pom.xml 中,但它不起作用。我如何告诉 Thymeleaf 使用它?
【问题讨论】:
标签: spring spring-boot thymeleaf
我正在使用 Spring Boot,我想添加 IE conditional comments Thymeleaf dialect。
我已将它包含在我的 maven pom.xml 中,但它不起作用。我如何告诉 Thymeleaf 使用它?
【问题讨论】:
标签: spring spring-boot thymeleaf
注意:在尝试之前,请注意更高版本的 Spring Boot 包含一些开箱即用的常见方言。请参阅@Robert Hunt 的answer。否则:
有一个示例 here 添加方言 bean,Spring Boot 将自动检测和使用它(请参阅 LayoutDialect 代码和 ThymeleafDefaultConfiguration 类的方言成员)。在您的情况下,在您的 @Configuration 类之一中添加以下内容:
@Bean
public ConditionalCommentsDialect conditionalCommentDialect() {
return new ConditionalCommentsDialect();
}
Spring Boot,在 ThymeleafAutoConfiguration 类中,会自动添加任何实现 IDialect 接口的 Bean。
【讨论】:
随着 Spring Boot 1.2.1 的发布,ThymeleafAutoConfiguration 类中添加了一些额外的方言,如果它们在类路径中,将被自动检测,包括:
只需在类路径中包含 JAR 就足以让 Spring Boot 注册它们。
注意:如果您使用的是spring-boot-starter-thymeleaf,那么您会发现默认情况下已经包含LayoutDialect。
【讨论】:
我实际上认为ThymeleafAutoConfiguration 存在缺陷。如果它在类路径上,我看到它应该拾取并将 SpringSecurityDialect 添加到配置的代码,但在我的调试中,这根本没有发生(只有 LayoutDialect 被定向并添加到配置中)。我的类路径上有 SpringSecurityDialect 类/jar,但 SpringBoot AutoConfig 从未将下面的 bean 添加到配置中(ThymeleafAutoConfig.java,第 97 行)
@Configuration
@ConditionalOnClass({SpringSecurityDialect.class})
protected static class ThymeleafSecurityDialectConfiguration {
protected ThymeleafSecurityDialectConfiguration() {
}
@Bean
@ConditionalOnMissingBean
public SpringSecurityDialect securityDialect() {
return new SpringSecurityDialect();
}
}
最后,我必须在我的自定义 Java 配置中添加一个 bean 才能识别 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>
【讨论】: