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());
    }
}


http://www.blogjava.net/sxyx2008/archive/2010/11/11/337799.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值