java Spring Boot 和 Thymeleaf 3.0.0.RELEASE 集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37439369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot and Thymeleaf 3.0.0.RELEASE integration
提问by przodownikPracy
I have a problem, when I try integrate Spring Boot 1.3.5.RELEASE and Thymeleaf 3.0.0.Release. I know that Spring Boot now support Thymeleaf 3 version so I try workaround this problem like this :
我有一个问题,当我尝试集成 Spring Boot 1.3.5.RELEASE 和 Thymeleaf 3.0.0.Release 时。我知道 Spring Boot 现在支持 Thymeleaf 3 版本,所以我尝试像这样解决这个问题:
@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
and add my own SpringWebConfig configuration. unfortunately received error like this :
并添加我自己的 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)
回答by Henrique Rocha
It's much simpler, just read this: http://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#howto-use-thymeleaf-3
它更简单,只需阅读: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>
回答by user6856044
spring boot 1.4.0 + thymeleaf 3.0.1
弹簧靴 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>
回答by mangei
As stated in the documentation for 1.4.7.RELEASE: https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#howto-use-thymeleaf-3
如 1.4.7.RELEASE 的文档所述:https://docs.spring.io/spring-boot/docs/1.4.7.RELEASE/reference/htmlsingle/#howto-use-thymeleaf-3
If you use Gradle put this in the build.gradle file:
如果您使用 Gradle,请将其放在 build.gradle 文件中:
Gradle: build.gradle
摇篮:build.gradle
ext['thymeleaf.version'] = '3.0.2.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.1.1'
If you use Maven put this in the pom.xml file:
如果您使用 Maven,请将其放在 pom.xml 文件中:
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>
(since I have not enough reputation, I posted an answer instead of a comment on one of the previous answers. Especially the build.gradle part.)
(由于我没有足够的声誉,我发布了一个答案而不是对之前的一个答案发表评论。尤其是 build.gradle 部分。)
回答by srsajid
For Maven project just add following line on 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>
For gradle project create a gradle.properties file with following contents
对于 gradle 项目,创建一个包含以下内容的 gradle.properties 文件
thymeleaf.version=3.0.2.RELEASE
thymeleaf-layout-dialect.version=2.1.1
回答by Crunch
I have Spring Boot 1.3.3 working with Thymeleaf 3 using this Configuration class. I recall having to work to get around that same exception. Also, ThymeleafAutoConfiguration is excluded in my autoscan setup, like it is in yours.
我使用此配置类将 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();
}
}
(The resolver.setPrefix, resolver.setSuffix, and resolver.setTemplateMode may not be necessary anymore, but they were with the first beta release.)
(resolver.setPrefix、resolver.setSuffix 和 resolver.setTemplateMode 可能不再需要,但它们在第一个 beta 版本中。)
回答by Vivek Gajbhiye
Do the simply step by step
做简单的一步一步
1. adding dependency for thymeleaf.
1. 为 thymeleaf 添加依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2. adding dependency for thymeleaf dialect.
2. 增加thymeleaf 方言的依赖。
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
3. SpringBoot autoconfiguration for thymeleaf
3. thymeleaf 的 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