Velocity中加載vm文件的三種方式

Velocity中加載vm文件的三種方式:

 

方式一:加載classpath目錄下的vm文件

Properties p = new Properties();

p.put("file.resource.loader.class",

"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");

Velocity.init(p);

...

Velocity.getTemplate(templateFile);

 

 

方式二:根據絕對路徑加載,vm文件置於硬盤某分區中,如:d://tree.vm

Properties p = new Properties();

p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "d://");

Velocity.init(p);

...

Velocity.getTemplate("tree.vm");

 

 

方式三:使用文本文件,如:velocity.properties,配置如下:

input.encoding = UTF-8

file.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

 

再利用如下方式進行加載

Properties p = new Properties();

p.load(this.getClass().getResourceAsStream("/velocity.properties"));

Velocity.init(p);

...

Velocity.getTemplate(templateFile);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章