velocity使用基本来说比较简单,但在加载模板时老出问题,很多初学者经常会遇到找不到模板这种异常。本文就针对目前常用的三种模板加载方式做以说明。
velocity官方帮助文档
其工程目录结构大致为
点我下载示例代码
一、velocity默认的加载方式(文件加载方式)
package com.velocity.test;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/**
* 从文件中加载模板文件,即velocity默认的模板文件加载方式
* @author welcome
*
*/
public class LoaderFromFile {
public static void main(String[] args) throws Exception{
//初始化参数
Properties properties=new Properties();
//设置velocity资源加载方式为file
properties.setProperty("resource.loader", "file");
//设置velocity资源加载方式为file时的处理类
properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
//实例化一个VelocityEngine对象
VelocityEngine velocityEngine=new VelocityEngine(properties);
//实例化一个VelocityContext
VelocityContext context=new VelocityContext();
//向VelocityContext中放入键值
context.put("username", "张三");
context.put("password", "123456789");
context.put("age", "20");
context.put("address", "陕西西安");
context.put("blog", "http://blogjava.net/sxyx2008");
//实例化一个StringWriter
StringWriter writer=new StringWriter();
//从vm目录下加载hello.vm模板,在eclipse工程中该vm目录与src目录平级
velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);
System.out.println(writer.toString());
}
}
二、从类路径加载模板文件
package com.velocity.test;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/**
* 从class(类路径)中加载模板文件
* @author welcome
*
*/
public class LoaderFromClass {
public static void main(String[] args) throws Exception{
//初始化参数
Properties properties=new Properties();
//设置velocity资源加载方式为class
properties.setProperty("resource.loader", "class");
//设置velocity资源加载方式为file时的处理类
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
//实例化一个VelocityEngine对象
VelocityEngine velocityEngine=new VelocityEngine(properties);
//实例化一个VelocityContext
VelocityContext context=new VelocityContext();
//向VelocityContext中放入键值
context.put("username", "张三");
context.put("password", "123456789");
context.put("age", "20");
context.put("address", "陕西西安");
context.put("blog", "http://blogjava.net/sxyx2008");
//实例化一个StringWriter
StringWriter writer=new StringWriter();
//从src目录下加载hello.vm模板
//假若在com.velocity.test包下有一个hello.vm文件,那么加载路径为com/velocity/test/hello.vm
velocityEngine.mergeTemplate("com/velocity/test/hello.vm", "gbk", context, writer);
//velocityEngine.mergeTemplate("hello.vm", "gbk", context, writer);
System.out.println(writer.toString());
}
}
三、从jar文件中加载模板文件
package com.velocity.test;
import java.io.StringWriter;
import java.util.Properties;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
/**
* 从jar文件中加载模板文件
* @author welcome
*
*/
public class LoaderFromJar {
public static void main(String[] args) throws Exception{
//初始化参数
Properties properties=new Properties();
//设置velocity资源加载方式为jar
properties.setProperty("resource.loader", "jar");
//设置velocity资源加载方式为file时的处理类
properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
//设置jar包所在的位置
properties.setProperty("jar.resource.loader.path", "jar:file:WebRoot/WEB-INF/lib/vm.jar");
//实例化一个VelocityEngine对象
VelocityEngine velocityEngine=new VelocityEngine(properties);
//实例化一个VelocityContext
VelocityContext context=new VelocityContext();
//向VelocityContext中放入键值
context.put("username", "张三");
context.put("password", "123456789");
context.put("age", "20");
context.put("address", "陕西西安");
context.put("blog", "http://blogjava.net/sxyx2008");
//实例化一个StringWriter
StringWriter writer=new StringWriter();
//从/WebRoot/WEB-INF/lib/vm.jar中加载hello.vm模板 vm.jar的目录结构为vm/hello.vm
velocityEngine.mergeTemplate("vm/hello.vm", "gbk", context, writer);
System.out.println(writer.toString());
}
}
请读者朋友自行运行程序。
点我下载示例代码