场景 : 公司旧项目重构,丛ssm到新框架,使用了sping boot ,需要重新整合下Freemark
也发生了 像
java.io.FileNotFoundException: Template "xxxxr.ftl" not found. 这样的bug
开始整合 首先架包
<!-- 弃用 freemarker jar -->
<!-- <dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
-->
<!-- 使用 springframework jar -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
配置文件
# freemarker静态资源配置
freemarker:
# 是否允许HttpServletRequest属性覆盖(隐藏)控制器生成的同名模型属性。
allow-request-override : false
# 是否允许HttpSession属性覆盖(隐藏)控制器生成的同名模型属性。
allow-session-override : false
# 是否启用模板缓存。
cache : false
# 模板编码。
charset : UTF-8
# 是否检查模板位置是否存在。
check-template-location : true
# Content-Type value.
content-type : text/html
# 是否启用freemarker
enabled : true
# 设定所有request的属性在merge到模板的时候,是否要都添加到model中.
expose-request-attributes : false
# 是否在merge模板的时候,将HttpSession属性都添加到model中
expose-session-attributes : false
# 设定是否以springMacroRequestContext的形式暴露RequestContext给Spring’s macro library使用
expose-spring-macro-helpers : true
# 是否优先从文件系统加载template,以支持热加载,默认为true
prefer-file-system-access : true
# 设定模板的后缀.
suffix : .ftl
# 设定模板的加载路径,多个以逗号分隔,默认:
template-loader-path : classpath:/templates
# 设定FreeMarker keys.
#settings.template_update_delay : 0
#settings.default_encoding : UTF-8
# settings.classic_compatible : true
调用打印
Template template = null;
Configuration cfg = SpringContextHolder.getBean(Configuration.class);
cfg.setDefaultEncoding("UTF-8");
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
cfg.setObjectWrapper(new DefaultObjectWrapper());
template = cfg.getTemplate(templateName);
System.err.println("===========触发邮箱========"+templateName);