2017-07-24 97 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");