我正在开发 Java/Spark 框架中的应用程序,并且正在使用 Apache Velocity 模板引擎。我的问题是,每次我更改模板中的任何内容时,我都必须重新加载整个服务器。有没有办法让某种热交换能够在模板上工作而无需重新加载整个服务器?
private final VelocityEngine velocityEngine;
public VelocityTemplateEngine() {
Properties properties = new Properties();
properties.setProperty("resource.loader", "class");
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
properties.setProperty("class.resource.loader.cache", "true");
properties.setProperty("class.resource.loader.modificationCheckInterval", "2");
properties.setProperty("velocimacro.library.autoreload", "true");
properties.setProperty("velocimacro.permissions.allow.inline.to.replace.global", "true");
velocityEngine = new org.apache.velocity.app.VelocityEngine(properties);
//velocityEngine.init(); <-- This bit of code does not change anything when uncommented...
}
解决方法:
将resource.loader改成file,class.resource.loader.class改成org.apache.velocity.runtime.resource.loader.FileResourceLoader解决
properties.setProperty("resource.loader", "file");
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
properties.setProperty("file.resource.loader.path", root + "/src/main/resources/"); // "root" points to the app folder
properties.setProperty("class.resource.loader.cache", "true"); // Enable cache
properties.setProperty("class.resource.loader.modificationCheckInterval", "2"); // Check for new files every 2 seconds