我想使用我的 Google App Engine java 应用程序部署一些 Freemarker 模板,以用作电子邮件正文模板。我正在使用 freemarker-gae-2.3.23.jar。
我的问题是我应该将模板文件放在 war 文件的什么位置,以便 Freemarker 配置类可以找到它们?我认为 WEB-INF/classes/templates 可以工作,但是当我在 GAE 实例上运行它时出现以下错误。 getRealPath() 也没有给出任何见解。返回空字符串。非常感谢任何想法或建议。
SEVERE: Template ./templates/invitation.ftl not found.
java.lang.RuntimeException: Error in loading ftl template: Template ./templates/invitation.ftl not found.
我的基本配置如下:
public class FreeMarkerConfig {
private static FreeMarkerConfig freeMarkerConfig = null;
private static Configuration cfg = null;
private static final Logger logger = Logger.getLogger(FreeMarkerConfig.class.getName());
private FreeMarkerConfig(ServletContext context){
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(context, "/templates");
}
public static FreeMarkerConfig getInstance(ServletContext context){
if(freeMarkerConfig == null){
freeMarkerConfig = new FreeMarkerConfig(context);
return freeMarkerConfig;
}
return freeMarkerConfig;
}
public static Template getTemplateByName(String fileName){
try {
return cfg.getTemplate(fileName);
} catch(IOException e) {
logger.severe(e.getMessage());
e.getStackTrace();
throw new RuntimeException("Error in loading ftl template: "+e.getMessage());
}
}
}l
最佳答案
解决方案是双重的。上下文位置是“web”目录。所以用这个设置 freemarker 配置,解决了这个问题。
private FreeMarkerConfig(ServletContext context){
cfg = new Configuration();
cfg.setServletContextForTemplateLoading(context, "/WEB-INF/classes/templates/");
}
作为第二个有用的提示,我发现我必须只使用模板名称而不是文件名来请求模板。即邀请而不是邀请.ftl
关于java - Google App Engine 上的 ServletContext 类路径中的 "context"在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32674882/