阅读量:2
配置Thymeleaf的方法有以下几步:
- 添加Thymeleaf依赖:在pom.xml文件中添加Thymeleaf的依赖配置。例如:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
- 配置Thymeleaf模板的位置:在application.properties或application.yml文件中添加以下配置项,指定Thymeleaf模板的位置,默认为
classpath:/templates/
。例如:
spring.thymeleaf.prefix=classpath:/templates/
- 配置Thymeleaf缓存:在application.properties或application.yml文件中添加以下配置项,用于配置Thymeleaf的缓存,默认为开启缓存。例如:
spring.thymeleaf.cache=false
- 配置Thymeleaf模板的后缀:在application.properties或application.yml文件中添加以下配置项,指定Thymeleaf模板的后缀,默认为
.html
。例如:
spring.thymeleaf.suffix=.html
- 配置Thymeleaf视图解析器:在配置类中添加
@Bean
注解配置Thymeleaf视图解析器。例如:
@Configuration public class ThymeleafConfig { @Bean public ViewResolver thymeleafViewResolver() { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine()); viewResolver.setCharacterEncoding("UTF-8"); return viewResolver; } @Bean public ITemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); return templateEngine; } @Bean public ITemplateResolver templateResolver() { ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setPrefix("templates/"); templateResolver.setCacheable(false); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setCharacterEncoding("UTF-8"); return templateResolver; } }
以上是配置Thymeleaf的基本步骤,根据具体需求可以添加其他配置项。