java 未找到 Freemarker 模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1799614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:02:04  来源:igfitidea点击:

Freemarker template not found

javaspringfreemarker

提问by brock

I'm currently trying to get Freemarker to work with my application using Spring. No matter what I try I keep getting template not found. I am not sure if I have the configuration set up properly, but it never finds my template. Here is my spring bean config:

我目前正在尝试让 Freemarker 使用 Spring 来处理我的应用程序。无论我尝试什么,我总是找不到模板。我不确定我是否正确设置了配置,但它从未找到我的模板。这是我的 spring bean 配置:

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/"/>
</bean>

Whenever I try to call getTemplate on the freemaker configuration it always sends back a template not found error. So if I do

每当我尝试在 freemaker 配置上调用 getTemplate 时,它​​总是会发回模板未找到错误。所以如果我这样做

configuration.getTemplate("testTemplate.ftl") 

it always throws an IOException.

它总是抛出一个 IOException。

I'm not sure if anyone has an idea of what I'm doing wrong.

我不确定是否有人知道我做错了什么。

Thanks for all your help!

感谢你的帮助!

采纳答案by ChssPly76

First of all, /WEB-INF/freemarkerwould only work as a path from within WebApplicationContext; otherwise Spring would attempt to resolve it as file system path rather than servlet context path. Is the excerpt you've posted above from the context being loaded by DispatcherServlet?

首先,/WEB-INF/freemarker只能作为从内部的路径WebApplicationContext;否则 Spring 会尝试将其解析为文件系统路径而不是 servlet 上下文路径。您在上面发布的摘录是否来自正在加载的上下文DispatcherServlet

Secondly, is there any reason why are you using configurationdirectly instead of using Spring's ViewResolver?

其次,你有什么理由configuration直接使用而不是使用 Spring 的ViewResolver吗?

Finally, IOExceptioncan mean many different things. Can you post a full stack trace?

最后,IOException可能意味着许多不同的事情。你能发布一个完整的堆栈跟踪吗?

回答by Luismy

I've just had the same kind of problem and, at the end, I decide to use the approach below:

我刚刚遇到了同样的问题,最后,我决定使用以下方法:

Configuration configuration = new Configuration();
FileTemplateLoader templateLoader = new FileTemplateLoader(new File(YOUR_BASE_TEMPLATE_DIR));
configuration.setTemplateLoader(templateLoader);
freemarker.template.Template template = configuration.getTemplate(YOUR_TEMPLATE_NAME);
template.process(datamodel, writer);

回答by Sunset Fun

I think you must make sure that the file "testTemplate.ftl" is in the folder "/WEB-INF/freemarker/"

我认为您必须确保文件“testTemplate.ftl”位于“/WEB-INF/freemarker/”文件夹中

回答by Alex

you can also set it like

您也可以将其设置为

    @Bean
    public FreeMarkerConfigurationFactoryBean freemarkerConfiguration() {
        FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
        bean.setTemplateLoaderPath("classpath:/templates/");
        return bean;
    }

In your case:

在你的情况下:

    <property name="templateLoaderPath" value="classpath:/WEB-INF/freemarker/"/>