springboot配置thymeleaf的方法是什么

avatar
作者
筋斗云
阅读量:2

配置Thymeleaf的方法有以下几步:

  1. 添加Thymeleaf依赖:在pom.xml文件中添加Thymeleaf的依赖配置。例如:
<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 
  1. 配置Thymeleaf模板的位置:在application.properties或application.yml文件中添加以下配置项,指定Thymeleaf模板的位置,默认为classpath:/templates/。例如:
spring.thymeleaf.prefix=classpath:/templates/ 
  1. 配置Thymeleaf缓存:在application.properties或application.yml文件中添加以下配置项,用于配置Thymeleaf的缓存,默认为开启缓存。例如:
spring.thymeleaf.cache=false 
  1. 配置Thymeleaf模板的后缀:在application.properties或application.yml文件中添加以下配置项,指定Thymeleaf模板的后缀,默认为.html。例如:
spring.thymeleaf.suffix=.html 
  1. 配置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的基本步骤,根据具体需求可以添加其他配置项。

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!