1Velocity中加载vm文件的三种方式 2 3velocitypropertiespath 4Velocity中加载vm文件的三种方式: 5 6方式一:加载classpath目录下的vm文件 7Properties p = new Properties(); 8p.put("file.resource.loader.class", 9"org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 10Velocity.init(p); 11... 12Velocity.getTemplate(templateFile); 13 14 15方式二:根据绝对路径加载,vm文件置于硬盘某分区中,如:d://tree.vm 16Properties p = new Properties(); 17p.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, "d://"); 18Velocity.init(p); 19... 20Velocity.getTemplate("tree.vm"); 21 22 23方式三:使用文本文件,如:velocity.properties,配置如下: 24#encoding 25input.encoding=UTF-8 26output.encoding=UTF-8 27contentType=text/html;charset=UTF-8 28不要指定loader. 29 30 31再利用如下方式进行加载 32Properties p = new Properties(); 33p.load(this.getClass().getResourceAsStream("/velocity.properties")); 34Velocity.init(p); 35... 36Velocity.getTemplate(templateFile); 37 38package com.study.volicity; 39 40import java.io.IOException; 41import java.io.StringWriter; 42import java.util.Properties; 43 44import org.apache.velocity.app.Velocity; 45import org.apache.velocity.Template; 46import org.apache.velocity.VelocityContext; 47 48public class Test { 49 50 51 public static void main(String args[]) throws IOException { 52 53 Properties pros = new Properties(); 54 pros.load(Test.class.getClassLoader().getResourceAsStream("velocity.properties")); 55 Velocity.init(pros); 56 VelocityContext context = new VelocityContext(); 57 58 context.put("name", "Velocity"); 59 context.put("project", "Jakarta"); 60 61 /* lets render a template 相对项目路径 */ 62 Template template = Velocity.getTemplate("/view/header.vm"); 63 64 StringWriter writer = new StringWriter(); 65 66 /* lets make our own string to render */ 67 template.merge(context, writer); 68 System.out.println(writer); 69 } 70 71}
Velocity中加载vm文件的三种方式
Wesley13
2021-10-11
1359 0 0
点赞
收藏
评论区
加载中...