Spring Boot和Thymeleaf 3.0.0.RELEASE集成

时间:2016-05-25 13:56:01

标签: java spring spring-boot thymeleaf

我遇到问题,当我尝试集成Spring Boot 1.3.5.RELEASE和Thymeleaf 3.0.0时。我知道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)

6 个答案:

答案 0 :(得分:17)

这简单得多,只需阅读: 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>

答案 1 :(得分:3)

spring boot 1.4.0 + thymeleaf 3.0.1

puts "$paths"
somethinghere/somethingthere/idontcare

答案 2 :(得分:2)

如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部分。)

答案 3 :(得分:1)

对于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

答案 4 :(得分:0)

我使用此配置类使用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,但它们是第一个测试版。)

答案 5 :(得分:0)

一步一步地完成

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