最近工做中须要用到模板解析的功能,找到了velocity,按照例子作了一遍,没有问题,
因而乎,直接用于工做环境,发现老是报Resource not found 的异常,
检查模板路径,并无写错什么,可是老是报这个异常,模板换了N个位置,仍是报此异常
后来抛弃
Velocity.init();
Template template = Velocity.getTemplate("container.htm");
改成:
VelocityEngine velocityEngine = new VelocityEngine();
Properties properties = new Properties();
String basePath = “路径/windows/tlp”;
properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
velocityEngine.init(properties);
Template template = Velocity.getTemplate("container.htm");
就能够了
看了一下文档,Velocity容许两种方式:
一、全局使用一个Velocity引擎(jvm中只有一个Velocity引擎的实例),适应于配置文件等解析(路径是一致的)
二、建立多个引擎实例,针对每一个引擎实例设置Velocity.FILE_RESOURCE_LOADER_PATH能够使用不一样位置的模板