【问题标题】:How do I add a thymeleaf dialect to spring boot?如何在弹簧靴中添加百里香方言?
【发布时间】:2014-06-25 05:51:06
【问题描述】:

我正在使用 Spring Boot,我想添加 IE conditional comments Thymeleaf dialect

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

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    注意:在尝试之前,请注意更高版本的 Spring Boot 包含一些开箱即用的常见方言。请参阅@Robert Hunt 的answer。否则:

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

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

    Spring Boot,在 ThymeleafAutoConfiguration 类中,会自动添加任何实现 IDialect 接口的 Bean。

    【讨论】:

    • 你为什么用第二人称回答自己的问题?
    • 你的评论花了我更长的时间......无论如何,谢谢分享!
    【解决方案2】:

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

    只需在类路径中包含 JAR 就足以让 Spring Boot 注册它们。

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

    【讨论】:

    • 我有点困惑,请您帮忙。 spring-boot-starter-parent 的最新版本是 1.5.2。使用此配置,thymeleaf 的版本解析为 2.something。而根据ultraq.github.io/thymeleaf-layout-dialect/Installation.html,布局方言需要thymeleaf 3.0
    • @djaqeel 2.0.0 版布局方言及更高版本支持 thymeleaf 3。该网站有点模棱两可,因为它说“需要 Java 7 和 Thymeleaf 3.0 的最低要求”,但在“迁移到 2.0 ” 页面底部的指南说“Thymeleaf 3 在很大程度上向后兼容 Thymeleaf 2 模板,因此布局方言也已竭尽全力向后兼容。”所以它可能值得一试。 Spring Boot 1.5.2 将“thymeleaf-layout-dialect”的依赖管理版本设置为版本 1.4.0,因此它可以与该版本正常工作。
    • 谢谢。是否有任何布局方言 1.4.0 教程的链接。因为ultraq.github.io/thymeleaf-layout-dialect/Examples.html#layouts 似乎对我不起作用。布局文件未应用。
    • @djaqeel 您最好将 thymeleaf 3 与 Spring Boot 1.5.2 一起使用,因为它确实支持它。如果您使用 Maven 并从 spring-boot-starter-parent 继承(或包括 BOM),只需定义一个属性: 3.0.3.RELEASE 您可能还需要设置 2.2.1 提升布局方言版本。
    【解决方案3】:

    我实际上认为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>
    

    【讨论】:

    • 它应该可以工作,而无需手动指定 bean。我没有使用 Spring Boot 1.2.5 对其进行测试,但在 1.2.1 中添加了支持,因此它应该可以工作。我确实检查了这个示例:github.com/spring-projects/spring-boot/tree/master/… 并添加了“thymeleaf-extras-springsecurity4”依赖项并自动注册。您可能希望使用 --debug 标志运行应用程序或添加 spring-boot-actuator 并阅读自动配置报告。
    • 谢谢,我在 1.3.3.RELEASE 中遇到了同样的问题,并且定义修复了它。它似乎在 1.4 中已修复。
    • 我仍然在线程“main”java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect 中收到异常,即使我添加了您的所有建议
    • @yglodt 使用版本 3.0.1.RELEASE 的 thymeleaf extras springsecurity 修复了这个问题。
    猜你喜欢
    • 2021-12-11
    • 2018-04-02
    • 2016-04-02
    • 1970-01-01
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    相关资源
    最近更新 更多