如何在弹簧靴中添加百里香方言?

xdh*_*ore 24 spring thymeleaf spring-boot

我正在使用Spring Boot,我想添加IE条件评论Thymeleaf方言.

我把它包含在我的maven pom.xml中,但它不起作用.我如何告诉Thymeleaf使用它?

xdh*_*ore 31

注意:在尝试此操作之前,请注意,Spring Boot的更高版本包含一些开箱即用的常用方言.请参阅@Robert Hunt的回答.除此以外:

有一个例子在这里加入方言豆类,其中春季启动会自动检测并使用(见LayoutDialect代码和ThymeleafDefaultConfiguration类的方言成员)的.在您的情况下,在其中一个@Configuration类中添加以下内容:

@Bean
public ConditionalCommentsDialect conditionalCommentDialect() {
    return new ConditionalCommentsDialect();
}
Run Code Online (Sandbox Code Playgroud)

Spring引导,在ThymeleafAutoConfiguration类中,将自动添加任何实现IDialect接口的Bean.

  • 你为什么在第二个人回答你自己的问题? (20认同)

Rob*_*unt 13

随着Spring Boot 1.2.1的发布,ThymeleafAutoConfiguration类中添加了一些额外的方言,如果它们位于类路径上,将自动检测,包括:

只需在类路径上使用JAR就足以让Spring Boot注册它们.

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

  • @djaqeel你可能最好使用带有Spring Boot 1.5.2的thymeleaf 3,因为它支持它.如果你正在使用Maven并继承spring-boot-starter-parent(或包括BOM),只需定义一个属性:<thymeleaf.version> 3.0.3.RELEASE </thymeleaf.version>你可能还需要设置< thymeleaf-layout-dialect.version> 2.2.1 </thymeleaf-layout-dialect.version>提升布局方言版本. (4认同)

小智 5

我实际上认为ThymeleafAutoConfiguration存在缺陷.我看到它应该拾取的代码并将SpringSecurityDialect添加到Config,如果它在类路径上,但在我的调试中,这根本不会发生(只有LayoutDialect被定向并添加到配置中).我的classpath上有SpringSecurityDialect类/ jar,但是下面的bean永远不会被SpringBoot AutoConfig添加到配置中(ThymeleafAutoConfig.java,第97行)

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

    @Bean
    @ConditionalOnMissingBean
    public SpringSecurityDialect securityDialect() {
        return new SpringSecurityDialect();
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,我必须实际将bean添加到我的自定义Java配置中以获得SpringSecurityDialog:

@Bean
public SpringSecurityDialect securityDialect() {
    return new SpringSecurityDialect();
}
Run Code Online (Sandbox Code Playgroud)

这是第一次工作.您是否可以通过一些测试来验证这是一个已知问题?我包括我的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>
Run Code Online (Sandbox Code Playgroud)