速度引擎:找不到模板资源



当我使用 Wildfly 服务器在 eclipse 中本地运行我的项目时,我能够访问模板。但是,当我在我的生产服务器上运行它时,它找不到模板。

我的 vm 模板位于包中:com.email.templates

我已经检查了 WEB-INF 文件夹,我可以看到模板位于:

myapp.warWEB-INFclassescomemailtemplates

我使用以下属性初始化我的速度引擎:

Properties prop = new Properties();
prop.put(RuntimeConstants.RESOURCE_LOADER, "class");
prop.put("class.resource.loader.class", ClasspathResourceLoader.class.getName());
this.velocityEngine.init(prop);

在尝试获取模板时,我已经尝试了以下两种方法:

viaorg.springframework.ui.velocity.VelocityEngineUtils

Map<String, String> model = new HashMap<String, String>();
model.put("payload", "some payload");
String body = VelocityEngineUtils.mergeTemplateIntoString(this.velocityEngine , "/com/email/templates/my_template.vm", model);

viaorg.apache.velocity.app.VelocityEngine

StringWriter writer = new StringWriter();
VelocityContext context = new VelocityContext();
context.put("payload", "some payload")
Template template = this.velocityEngine.getTemplate("/com/email/templates/my_template.vm");
template.merge(context, writer);

您的模板不在类路径中 您应该在资源文件夹中而不是web app中具有模板。这是工作我相信你需要有WebappResourceLoader

Properties props = new Properties();
props.setProperty("resource.loader", "webapp");
props.setProperty("myapp.resource.loader.class", "org.apache.velocity.tools.view.WebappResourceLoader");
props.setProperty("webapp.resource.loader.path", "classescomemailtemplates");
VelocityEngine engine = new VelocityEngine(props); 

编辑:如果您从资源文件夹加载,请始终使用以下代码:

velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "class,file");
velocityEngine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute");
velocityEngine.setProperty("runtime.log.logsystem.log4j.logger", "VELLOGGER");
velocityEngine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
velocityEngine.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");

最新更新