SpringBoot 无法加载 spring.ftl

SpringBoot 版本

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>

freemarker配置

spring:
  freemarker:
    enabled: true
    content-type: text/html
    charset: utf-8
    suffix: .ftl
    request-context-attribute: request
    expose-request-attributes: true
    expose-session-attributes: true
    check-template-location: true
    # 暴露 spring 提供的宏
    expose-spring-macro-helpers: true
    template-loader-path:
      - classpath:/templates/
      - classpath:/email/
    settings:
      datetime_format: yyyy-MM-dd HH:mm:ss

在视图(test/test.ftl)中尝试导入 spring.ftl 异常

org/springframework/web/servlet/view/freemarker/spring.ftl

<#import "spring.ftl" as spring/>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
	</body>
</html>

异常信息

[ERROR][XNIO-1 task-1][2019-06-06 18:22:52][freemarker.log._JULLoggerFactory$JULLogger error] => Error executing FreeMarker template
freemarker.core._MiscTemplateException: Template importing failed (for parameter value "spring.ftl"):
Template not found for name "test/spring.ftl".
The name was interpreted by this TemplateLoader: MultiTemplateLoader(loader1 = FileTemplateLoader(baseDir="D:\project\chat\target\classes\templates", canonicalBasePath="D:\project\chat\target\classes\templates\"), loader2 = FileTemplateLoader(baseDir="D:\project\chat\target\classes\email", canonicalBasePath="D:\project\chat\target\classes\email\"), loader3 = ClassTemplateLoader(resourceLoaderClass=org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer, basePackagePath="" /* relatively to resourceLoaderClass pkg */)).

----
FTL stack trace ("~" means nesting-related):

奇怪的是,日志显示的信息来看,classpath模板加载器ClassTemplateLoader 是存在的。而且加载的目录也正确。为啥会加载不到spring.ftl宏!!!

鼓捣明白了。加载的宏路径,如果不是以 / 开头,则认为是相对路径,则在当前目录查找
解决:使用绝对路径,添加 /

<#import "/spring.ftl" as spring/>

:sleepy:

说真的,这个社区的正文部分做的像评论一样,整体花里胡哨的东西太多了,打眼一看感觉到处都是东西,然而以前的社区的markdown编辑器有毛病