我有一个 Velocity 模板ValueTmpl.vm,它无法被 Velocity ResourceManager 找到。一个最小的例子:
package generate;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
public class Generate {
public static void main(String[] args) throws Exception {
VelocityContext context = new VelocityContext();
context.put("key", "value");
Writer writer = new FileWriter(new File("Result.java"));
createTemplate("generate/templates/ValueTmpl.vm").merge(context, writer);
writer.close();
}
private static Template createTemplate(String vmTemplateFile) {
VelocityEngine ve = new VelocityEngine();
ve.setProperty(Velocity.RESOURCE_LOADER, "class");
ve.setProperty("class.resource.loader.class",
"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
ve.init();
return ve.getTemplate(vmTemplateFile);
}
}
生成文件夹位于src目录的根目录中。我收到以下错误:
21.03.2011 13:09:01 org.apache.velocity.runtime.log.JdkLogChute log
SEVERE: ResourceManager : unable to find resource 'generate/templates/ValueTmpl.vm' in any resource loader.
Exception in thread "main" org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'generate/templates/ValueTmpl.vm'
有人知道问题可能是什么吗?我应该更改项目设置中的某些内容吗?
提前致谢!