Thymeleaf

这就是自动装配的原理


1) .SpringBoot启动会加载大量的自动配置类
2)、我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;
3)、我们再来看这个自动配置类中到底配置了哪些组件;(只要我们要用的组件存在在其中,我们就不需要再手动配置了)
4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。我们只需要在配置文件中指定这些属性的值即可;

xxxxAutoConfigurartion:自动配置类;给容器中添加组件
xxxxProperties:封装配置文件中相关属性;

Thymeleaf

Spring Boot 推荐使用 Thymeleaf 作为其模板引擎。SpringBoot 为 Thymeleaf 提供了一系列默认配置,项目中一但导入了 Thymeleaf 的依赖,相对应的自动配置 (ThymeleafAutoConfiguration) 就会自动生效,因此 Thymeleaf 可以与 Spring Boot 完美整合


    org.thymeleaf
    thymeleaf
    3.0.15.RELEASE

ThymeleafAutoConfiguration 使用 @EnableConfigurationProperties 注解导入了 ThymeleafProperties 类,该类包含了与 Thymeleaf 相关的自动配置属性,其部分源码如下。

纯文本复制

  1. @ConfigurationProperties(
  2. prefix = "spring.thymeleaf"
  3. )
  4. public class ThymeleafProperties {
  5. private static final Charset DEFAULT_ENCODING;
  6. public static final String DEFAULT_PREFIX = "classpath:/templates/";
  7. public static final String DEFAULT_SUFFIX = ".html";
  8. private boolean checkTemplate = true;
  9. private boolean checkTemplateLocation = true;
  10. private String prefix = "classpath:/templates/";
  11. private String suffix = ".html";
  12. private String mode = "HTML";
  13. private Charset encoding;
  14. private boolean cache;
  15. ...
  16. }

 

Thymeleaf中的一些用法

Thymeleaf_第1张图片

根据以上配置属性可知,Thymeleaf 模板的默认位置在 resources/templates 目录下,默认的后缀是 html,即只要将 HTML 页面放在“classpath:/templates/”下,Thymeleaf 就能自动进行渲染。

与 Spring Boot 其他自定义配置一样,我们可以在 application.properties/yml 中修改以 spring.thymeleaf 开始的属性,以实现修改 Spring Boot 对 Thymeleaf 的自动配置的目的。

你可能感兴趣的:(spring,boot,后端,java)