不知道在带有Spring MVC的Freemarker html模板中在哪里加载模板“spring.ftl”

Don't know where to load template "spring.ftl" in Freemarker html template with Spring MVC

提问人:Ousmane MBINTE 提问时间:11/14/2023 最后编辑:Ousmane MBINTE 更新时间:11/14/2023 访问量:45

问:

我在我的 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>

模板生成器代码:

public static byte[] generateDoc(String templateName, String templateDocument, Map<String, Object> objects, Map<String, Object> params) throws IOException, TemplateException {
        Map<String, Object> ownParams = checkParams(params);
        Configuration config = getConfig();
        ConverterProperties converterProperties = getConverterProperties();

        Template template = new Template(templateName, new StringReader(templateDocument), config);

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
            Writer writer = new OutputStreamWriter(out);

            Environment env = template.createProcessingEnvironment(objects, writer);
            populateEnvironnment(env);
            env.process();

            ByteArrayOutputStream fos = new ByteArrayOutputStream();
            PdfDocument output = new PdfDocument(new PdfWriter(fos));
            output.setDefaultPageSize((PageSize) ownParams.get(PAGE_SIZE));

            try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
                HtmlConverter.convertToPdf(in, output, converterProperties);
            }
            return fos.toByteArray();
        }
    }
Java Spring-MVC FreeMarker

评论

0赞 M. Deinum 11/14/2023
您如何生成 PDF?请添加完整的堆栈跟踪(而不仅仅是自由标记部分)。
0赞 Ousmane MBINTE 11/14/2023
我检查了我的战争,在 WEB-INF 中我没有任何视图文件夹
0赞 Ousmane MBINTE 11/14/2023
我添加了模板生成器代码
0赞 M. Deinum 11/14/2023
看起来该代码没有使用您在那里的任何配置。viewresolver 几乎毫无用处,因为它用于视图渲染,而不是用于 PDF 生成。
0赞 M. Deinum 11/14/2023
此外,如果您不渲染视图,我认为您无法从中加载。因此,您需要将文件放在类路径中而不是 ./WEB-INF/views.ftl/WEB-INF

答: 暂无答案