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
宏!!!