【spring boot】第4篇:spring boot对模板引擎的支持

spring boot中支持哪些模板引擎

freemarker
thymeleaf

spring boot 整合 freemarker
添加场景启动器

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
spring boot 如何配置 freemarker

FreeMarkerAutoConfiguration :自动配置,给容器中添加 freemarker 相关组件
FreeMarkerProperties :配置 freemarker 的相关属性

@ConfigurationProperties(prefix = “spring.freemarker”)
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
//模板文件存放的路径,存放在该默认路径的文件 freemarker会自动渲染
public static final String DEFAULT_TEMPLATE_LOADER_PATH = “classpath:/templates/”;

public static final String DEFAULT_PREFIX = “”;
//模板文件默认后缀,可以在属性文件中配置覆盖
public static final String DEFAULT_SUFFIX = “.ftl”;
}
freemarker 语法介绍
spring boot 整合 thymeleaf
pom.xml中添加依赖
<!–配置启动器–>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring‐boot‐starter‐thymeleaf</artifactId>
</dependency>

<!–修改thymeleaf的版本号–>
properties>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!‐‐ 布局功能的支持程序 thymeleaf3 则需要 layout2 以上版本 ‐‐>
<thymeleaf‐layout‐dialect.version>2.2.2</thymeleaf‐layout‐ dialect.version>
</properties>
thymeleaf的属性配置类
@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”;
}

utf-8 编码文件
使用html文件
把html页面放在classpath:/templates/中就能自动渲染

thymeleaf 语法
官方文档

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理