我在 Srping 应用程序中使用 Freemarker 模板从 HTML 模板生成 PDF 文件。 所有模板均以 Base64 编码并保存在数据库中。因此,为了生成 PDF 文件,我加载 Base64 文档并调用 Freemarker。 在处理过程中我收到此错误:
----
FTL stack trace ("~" means nesting-related):
- Failed at: #import "/spring.ftl" as spring [in template "my_template.pdf" at line 5, column 1]
----
Caused by: freemarker.template.TemplateNotFoundException: Don't know where to load template "spring.ftl" from because the "template_loader" FreeMarker setting wasn't set (Configuration.setTemplateLoader), so it's null.
at freemarker.template.Configuration.getTemplate(Configuration.java:2957) ~[freemarker-2.3.32.jar:2.3.32]
at freemarker.core.Environment.getTemplateForInclusion(Environment.java:3062) ~[freemarker-2.3.32.jar:2.3.32]
at freemarker.core.Environment.getTemplateForInclusion(Environment.java:3024) ~[freemarker-2.3.32.jar:2.3.32]
at freemarker.core.Environment.getTemplateForImporting(Environment.java:3186) ~[freemarker-2.3.32.jar:2.3.32]
at freemarker.core.Environment.importLib(Environment.java:3171) ~[freemarker-2.3.32.jar:2.3.32]
at freemarker.core.Environment.importLib(Environment.java:3135) ~[freemarker-2.3.32.jar:2.3.32]
at freemarker.core.LibraryLoad.accept(LibraryLoad.java:65) ~[freemarker-2.3.32.jar:2.3.32]
你可以找到我的 Freemarker 配置:
package com.omb;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import java.util.Properties;
@Configuration
@EnableWebMvc
public class FreeMarkerConfiguration implements WebMvcConfigurer {
@Bean
public FreeMarkerViewResolver viewResolver() {
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
viewResolver.setExposeSpringMacroHelpers(true);
viewResolver.setExposeRequestAttributes(true);
viewResolver.setPrefix("");
viewResolver.setSuffix(".ftl");
viewResolver.setContentType("text/html;charset=UTF-8");
return viewResolver;
}
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setDefaultEncoding("UTF-8");
configurer.setTemplateLoaderPath("/WEB-INF/views/");
Properties settings = new Properties();
settings.setProperty("auto_import", "/spring.ftl as spring");
configurer.setFreemarkerSettings(settings);
return configurer;
}
}
模板 html
<#import "/spring.ftl" as spring/>
<!DOCTYPE html>
<html>
<body>
<div><@spring.message "MESSAGE_FROM_RESSOURCE_BUNDLE"/></div>
</body>
</html>
这个错误是可以理解的。
解决此问题的两种方法:
从配置器中完全删除设置。您根本不需要设置它,因为您已经在模板 html 文件中导入了 /spring.ftl 。
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setDefaultEncoding("UTF-8");
configurer.setTemplateLoaderPath("/WEB-INF/views/");
return configurer;
}
或者尝试在设置属性变量中设置
template_loader
。
@Bean
public FreeMarkerConfigurer freemarkerConfig() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setDefaultEncoding("UTF-8");
configurer.setTemplateLoaderPath("/WEB-INF/views/");
Properties settings = new Properties();
settings.setProperty("auto_import", "/spring.ftl as spring");
settings.setProperty("template_loader", "/WEB-INF/views/");
configurer.setFreemarkerSettings(settings);
return configurer;
}