【问题标题】:Spring Boot and Thymeleaf 3.0.0.RELEASE integrationSpring Boot 和 Thymeleaf 3.0.0.RELEASE 集成
【发布时间】:2016-09-23 04:33:10
【问题描述】:

当我尝试集成 Spring Boot 1.3.5.RELEASE 和 Thymeleaf 3.0.0.Release 时,我遇到了一个问题。我知道 Spring Boot 现在支持 Thymeleaf 3 版本,所以我尝试像这样解决这个问题:

@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})

并添加我自己的 SpringWebConfig 配置。 不幸收到这样的错误:

java.lang.ClassNotFoundException: org.thymeleaf.resourceresolver.IResourceResolver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_66]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_66]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_66]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_66]
    ... 37 common frames omitted
Wrapped by: java.lang.NoClassDefFoundError: org/thymeleaf/resourceresolver/IResourceResolver



wrapped by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration due to org/thymeleaf/resourceresolver/IResourceResolver not found. M                                                                                                        ake sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

【问题讨论】:

  • Spring Boot 1.3 不支持(根据您的支持或您的措辞不支持,请更改您的问题)。还要添加完整的堆栈跟踪,不仅是 sn-p 以及您的配置。

标签: java spring spring-boot thymeleaf


【解决方案1】:

这要简单得多,只需阅读以下内容: http://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#howto-use-thymeleaf-3

<properties>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

【讨论】:

  • 您可能还想添加 3.0.0.RELEASE
【解决方案2】:

弹簧靴 1.4.0 + thymeleaf 3.0.1

    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>3.0.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
        <version>2.0.3</version>
    </dependency>

【讨论】:

  • 请同时添加一些解释上面的代码是如何解决问题的。
  • 这有帮助。案例:Spring Boot 被用作 POM-import 依赖,而不是父 POM。
  • 更好地使用 Henrique 答案的属性
【解决方案3】:

如 1.4.7.RELEASE 文档中所述: https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#howto-use-thymeleaf-3


如果您使用 Gradle,请将其放入 build.gradle 文件中:

Gradle:build.gradle

ext['thymeleaf.version'] = '3.0.2.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.1.1'

如果您使用 Maven,请将其放入 pom.xml 文件中:

Maven:pom.xml

<properties>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

(由于我没有足够的声誉,我发布了一个答案,而不是对以前的答案之一的评论。尤其是 build.gradle 部分。)

【讨论】:

    【解决方案4】:

    对于 Maven 项目,只需在 pom.xml 中添加以下行

    <properties>
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
    </properties>
    

    为 gradle 项目创建一个 gradle.properties 文件,内容如下

    thymeleaf.version=3.0.2.RELEASE
    thymeleaf-layout-dialect.version=2.1.1
    

    【讨论】:

      【解决方案5】:

      我有 Spring Boot 1.3.3 使用这个配置类与 Thymeleaf 3 一起工作。我记得必须努力解决同样的异常。此外,ThymeleafAutoConfiguration 不包括在我的自动扫描设置中,就像在你的设置中一样。

      @Configuration
      @EnableConfigurationProperties(ThymeleafProperties.class)
      @ConditionalOnClass(SpringTemplateEngine.class)
      @AutoConfigureAfter(WebMvcAutoConfiguration.class)
      
      public class ThymeleafConfiguration extends WebMvcConfigurerAdapter implements ApplicationContextAware {
      
          private ApplicationContext applicationContext;
      
          @Autowired
          ThymeleafProperties properties;
      
          public void setApplicationContext(ApplicationContext applicationContext) {
              this.applicationContext = applicationContext;
          }
      
          @Bean
          public ViewResolver viewResolver() {
              ThymeleafViewResolver resolver = new ThymeleafViewResolver();
              resolver.setTemplateEngine(templateEngine());
              resolver.setCharacterEncoding("UTF-8");
              return resolver;
          }
      
          private TemplateEngine templateEngine() {
              SpringTemplateEngine engine = new SpringTemplateEngine();
              engine.addTemplateResolver(urlTemplateResolver());
              engine.addTemplateResolver(templateResolver());
              // pre-initialize the template engine by getting the configuration.  It's a side-effect.
              engine.getConfiguration();
              return engine;
          }
      
          private ITemplateResolver templateResolver() {
              SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
              resolver.setApplicationContext(applicationContext);
              resolver.setPrefix("classpath:templates/");
              resolver.setSuffix(".html");
              resolver.setTemplateMode(TemplateMode.HTML);
              resolver.setCacheable(properties.isCache());
              return resolver;
          }
      
          private UrlTemplateResolver urlTemplateResolver() {
              return new UrlTemplateResolver();
          }
      
      }
      

      (resolver.setPrefix、resolver.setSuffix 和 resolver.setTemplateMode 可能不再需要,但它们在第一个 beta 版本中。)

      【讨论】:

      • 非常感谢您的帮助,我对这种配置和集成 Thymeleaf 和 Spring Boot 感到有些困惑。但是问题依然存在。... 暂时在目标项目中回一个以前版本的thymeleaf。下面是此解决方案概念证明的链接。有完整的配置和依赖关系。 [链接]github.com/przodownikR1/voteEngine
      【解决方案6】:

      一步一步做简单的事

      1.为百里香添加依赖。

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-thymeleaf</artifactId>
          </dependency>
      

      2。添加对百里香方言的依赖。

          <dependency>
              <groupId>nz.net.ultraq.thymeleaf</groupId>
              <artifactId>thymeleaf-layout-dialect</artifactId>
          </dependency>
      

      3.百里香的 SpringBoot 自动配置

      spring.thymeleaf.cache=false
      spring.thymeleaf.check-template=true 
      spring.thymeleaf.check-template-location=true
      spring.thymeleaf.enabled=true 
      spring.thymeleaf.servlet.content-type=text/html
      spring.thymeleaf.prefix=/WEB-INF/html/
      spring.thymeleaf.suffix=.html
      spring.thymeleaf.encoding=UTF-8
      spring.thymeleaf.mode=HTML
      spring.thymeleaf.reactive.max-chunk-size=0
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-08
        • 1970-01-01
        • 2021-09-19
        • 2020-08-05
        • 1970-01-01
        • 2020-08-16
        • 1970-01-01
        • 2022-12-30
        相关资源
        最近更新 更多