2017-07-24 54 views
0

当我在eclipse中使用Wildfly服务器在本地运行项目时,我能够访问这些模板。但是,当我在prod服务器上运行它时,它无法找到模板。VelocityEngine:找不到模板资源

我的VM模板坐在一个包:com.email.templates

我已检查WEB-INF文件夹,我可以看到,模板坐在:

myapp.war\WEB-INF\classes\com\email\templates\ 

我初始化我的VelocityEngine具有以下属性:

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

当试图获取t时,我试过以下两种方法下摆模板:

通过org.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); 

通过org.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); 
+0

是否使用'Maven'? 。看起来你的路径不正确。 – soorapadman

+0

我添加了prop.setProperty(“runtime.log.logsystem.class”,“org.apache.velocity.runtime.log.CommonsLogLogChute”)。这似乎解决了这个问题。 – Fabii

回答

0

您的模板不是在classpath您应该在资源文件夹中有模板,而不是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", "classes\com\email\templates"); 
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");