无法使用multipartResolver加载freemarker模板 [英] Unable to load freemarker template using multipartResolver
问题描述
我正在使用通过类路径在Spring MVC应用程序中配置的MultipartFile:
I am using MultipartFile configured in my Spring MVC app via the classpath:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="1000000"/>
</bean>
<bean id="MyController" class="myController">
<property name="myTemplate" value="classpath:myTemplate.txt"/>
</bean>
我正在尝试使用freemarker来加载此模板:
And I am trying to use freemarker in order to load this templete:
public class MyController
{
private Resource myTemplate;
....
Configuration cfg = new Configuration();
Template tpl = cfg.getTemplate(myTemplate.getFilename());
但是当我运行它时,我得到一个错误:找不到模板类路径:myTemplate.txt
But when I run it I am getting an error: Template classpath:myTemplate.txt not found
我尝试使用:cfg.setDirectoryForTemplateLoading(myTemplate.getFile().getParentFile())
确定目录,但这也无济于事.
I tried using: cfg.setDirectoryForTemplateLoading(myTemplate.getFile().getParentFile())
to determine the directory but that didn't help either.
有什么想法吗??
推荐答案
从引用的错误消息中,我想myTemplate.getFilename()
返回classpath:myTemplate.txt
,而不是myTemplate.txt
.这将说明为什么即使将模板目录设置为myTemplate.getFile().getParentFile()
,它也无法正常工作.
From the quoted error message I suppose myTemplate.getFilename()
returns classpath:myTemplate.txt
, not myTemplate.txt
. That would exaplain why it doesn't work even if you set the template directory to myTemplate.getFile().getParentFile()
.
但是这里最根本的问题是您似乎滥用了FreeMarker API.通常,应在应用程序生命周期中创建一次Configuration
对象.这是因为它包含模板缓存.如果仍然不使用模板缓存,则最好将模板加载到String
中,并使用Template
类构造函数创建Template
. (但是,如果它是#include
-s或#import
-s其他模板,则该FreeMarker将在内部调用cfg.getTemplate
.)如果要使用cfg.getTemplate
(因此也要使用缓存),则可能是应该使用可以理解这些Spring资源名称的TemplateLoader
,然后将FreeMarker配置为使用该名称(cfg.setTemplateLoader(yourTemplateLoader)
).我不知道Spring是否已经包含这样的东西,但是总的来说,将存储API包装到TemplateLoader
接口并不难. (从长远来看,肯定比尝试诱骗" FreeMarker使用临时的setDirectoryForTemplateLoading
等加载模板更容易和更可靠.)
But the more fundamental problem here is that you seem to misuse the FreeMarker API. The Configuration
object should be created once in the application life cycle, normally. That's among others because it contains the template cache. If you don't use the template cache anyway, then you might as well load the template into a String
, and create the Template
with the Template
class constructor. (However, if it #include
-s or #import
-s other templates, then for that FreeMarker will call cfg.getTemplate
internally.) If you want to use cfg.getTemplate
(and thus the cache too), then probably you should use a TemplateLoader
that understands those Spring resource names, then configure FreeMarker to use that (cfg.setTemplateLoader(yourTemplateLoader)
). I don't know if Spring already contains such thing or not, but in general it's not difficult to wrap a storage API into the TemplateLoader
interface. (It's certainly easier and more robust on the long run than trying to "trick" FreeMarker into loading a template with an ad-hoc setDirectoryForTemplateLoading
and such.)
这篇关于无法使用multipartResolver加载freemarker模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!