参考文档

boot-features-spring-mvc-template-engines:https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-template-engines

freemarker官方文档(英文):https://freemarker.apache.org/docs/

freemarker官方文档(中文):http://freemarker.foofun.cn/index.html

Spring Boot的properties:https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/htmlsingle/#templating-properties

加载方式

可以加载以下模板:

spring-boot-starter-freemarker
spring-boot-starter-groovy-templates
spring-boot-starter-hateoas
spring-boot-starter-thymeleaf

加载方式如下

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

Properties的解释

# Whether to enable MVC view resolution for this technology.
# 是否启用MVC视图解析。
spring.freemarker.enabled true

# Whether to enable template caching.
# 是否使用模板缓存
spring.freemarker.cache false

# Whether to prefer file system access for template loading. File system access enables hot detection of template changes.
# 是否更喜欢使用文件系统访问进行模板加载。 通过文件系统访问,可以热检测模板更改。
spring.freemarker.prefer-file-system-access true

# Whether HttpServletRequest attributes are allowed to override (hide) controller generated model attributes of the same name.
# 是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。
spring.freemarker.allow-request-override false

# Whether HttpSession attributes are allowed to override (hide) controller generated model attributes of the same name.
# 是否允许HttpSession属性覆盖(隐藏)控制器生成的同名模型属性。
spring.freemarker.allow-session-override false

# Template encoding.
# 模板的编码
spring.freemarker.charset UTF-8

# Whether to check that the templates location exists.
# 是否检查模板位置是否存在。
spring.freemarker.check-template-location true

# Content-Type value.
spring.freemarker.content-type text/html

# Whether all request attributes should be added to the model prior to merging with the template.
# 在与模板合并之前是否应将所有请求属性添加到模型。
spring.freemarker.expose-request-attributes false

# Whether all HttpSession attributes should be added to the model prior to merging with the template.
# 在与模板合并之前是否应将所有HttpSession属性添加到模型中。
spring.freemarker.expose-session-attributes false

# Whether to expose a RequestContext for use by Spring's macro library, under the name "springMacroRequestContext".
# 是否公开名称为“ springMacroRequestContext”的Spring宏库使用的RequestContext。
spring.freemarker.expose-spring-macro-helpers true

# Prefix that gets prepended to view names when building a URL.
# 前缀,用于在构建URL时查看名称。
spring.freemarker.prefix

# Name of the RequestContext attribute for all views.
# 所有视图的RequestContext属性的名称。
spring.freemarker.request-context-attribute

# Well-known FreeMarker keys which are passed to FreeMarker's Configuration.
# 众所周知的FreeMarker密钥被传递到FreeMarker的配置中。
spring.freemarker.settings.*

# Suffix that gets appended to view names when building a URL.
# 建立网址时,后缀会附加到视图名称中。
spring.freemarker.suffix .ftlh

# Comma-separated list of template paths.
# 逗号分隔的模板路径列表。
spring.freemarker.template-loader-path classpath:/templates/

# White list of view names that can be resolved.
# 可以解析的视图名称的白名单。
spring.freemarker.view-names

使用

路径说明

Freemarker的默认模板存放位置是classpath:/templates/,

默认情况下,Spring Boot从类路径中的/ static目录(或/ public或/ resources或/ META-INF / resources)或ServletContext的根目录中提供静态内容。