springboot配置thymeleaf的方法是什么

   2025-02-15 2330
核心提示:配置Thymeleaf的方法有以下几步:添加Thymeleaf依赖:在pom.xml文件中添加Thymeleaf的依赖配置。例如:dependencygroupIdorg.spr

配置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视图解析器。例如:
@Configurationpublic 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的基本步骤,根据具体需求可以添加其他配置项。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言