返回介绍

模版引擎?

发布于 2025-03-08 13:29:56 字数 2186 浏览 0 评论 0 收藏 0

看别人写的博客中谈到了一个叫 thymeleaf 的模板引擎,查了一番,发现好像就是一个在前后端不分离的情况下数据渲染的工具吧。有查到一个概念 ——前后端分离,通过 vue.js+SpringBoot 作前后端分离,通过 fetch ,ajax 等异步请求 java 封装的数据接口。高内聚低耦合,想想还是前后端分离一切组件化更优秀,所以模版引擎只是简单看一下

Thymeleaf 模版引擎,官方推荐的视图层模板引擎,提供了对 Thymeleaf 的自动化配置解决方案。需要在 pom 中添加该依赖,如下:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

以下是 ThymeleafProperties 配置类部分源码

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

  private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

  public static final String DEFAULT_PREFIX = "classpath:/templates/";

  public static final String DEFAULT_SUFFIX = ".html";

  /**
   * Whether to check that the template exists before rendering it.
   */
  private boolean checkTemplate = true;

  /**
   * Whether to check that the templates location exists.
   */
  private boolean checkTemplateLocation = true;

  /**
   * Prefix that gets prepended to view names when building a URL.
   */
  private String prefix = DEFAULT_PREFIX;
  //...
}

下图是类中几个属性的默认值,可以在 application.properties 中通过前缀 spring.thymeleaf.xxx 自定义配置

属性默认值含义
checkTemplatetrue注册前是否检测模版存在
checkTemplateLocationtrue是否检测模版存在
prefixclasspath:/templates/模版位置
suffix.html后缀
modeHTML模版模型
encodingStandardCharsets.UTF_8文件编码
cachetrue模版是否缓存
   
   
   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文